16+-+Interfaces+and+Invariants

16+-+Interfaces+and+Invariants - 9/1/10 ADTs Recall Recall...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
9/1/10 1 Interfaces and Invariants EECS 280 Programming and Introductory Data Structures 1 ADTs Recall 2 Recall the two main advantages of an ADT: 1. Information hiding : we don't need to know the details of how the object is represented, nor do we need to know how the operations on those objects are implemented. 2. Encapsulation : the objects and their operations are defined in the same place; the ADT combines both data and operation in one entity. ADTs Recall 3 To the caller, an ADT is only an interface. Here are two definitions: Good Definition : The contract for using things of this type. Better Definition : The set of assumptions that each programmer needs to make about another's code in order to demonstrate the correctness of his/her own code. Once you have an interface, you can pick from among many possible implementations as long as you satisfy the (implied) contract. Interfaces Separating out the details 4 The class mechanism, as we've used it so far, has one shortcoming: It co-mingles details of the implementation with the definition of the interface . Recall that the implementation of a class includes: 1. Data members 2. Method implementations The method implementations can be written separately from the class definition and usually are. Interfaces Separating out the details 5 For example, if we were really building an IntSet , we'd split it across two files: intset.h – the class IntSet {. ..} definition intset.cpp – the implementation of each method Unfortunately, the data members still must be part of the class definition. Since any programmer using an IntSet must see that definition, those programmers know something about the implementation. However, in a well-designed class, any implementation-specific bits must be either private or (less preferably) protected members. So, client code can't touch those objects. Interfaces Separating out the details 6 Having data objects in the definition has two undesirable effects: 1. It complicates the class definition, making it harder to read and understand. 2. It communicates information to the programmer that s/he isn't supposed to know. The second problem can have very drastic consequences. If a programmer using your class (mistakenly) makes an assumption about a "guarantee" that your implementation provides, but the interface doesn't promise, you're in trouble. What if you change the implementation?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9/1/10 2 Interfaces Separating out the details 7 Question : How can you provide a class definition that carries no implementation details to the client programmer, yet still has interface information? Remember, classes must contain their data members, so this means that the class cannot have an implementation.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 6

16+-+Interfaces+and+Invariants - 9/1/10 ADTs Recall Recall...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online