{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


14+-+Interfaces+and+Invariants - ADTs Recall EECS 280...

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

View Full Document Right Arrow Icon
11/8/2009 1 Interfaces and Invariants EECS 280 Programming and Introductory Data Structures 1 ADTs Recall 2 circle5 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 circle5 To the caller, an ADT is only an interface. circle5 Here are two definitions: circle5 Good Definition : The contract for using things of this type. circle5 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. circle5 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 circle5 The class mechanism, as we've used it so far, has one shortcoming: circle5 It co-mingles details of the implementation with the definition of the interface . circle5 Recall that the implementation of a class includes: 1. Data members 2. Method implementations circle5 The method implementations can be written separately from the class definition and usually are. Interfaces Separating out the details 5 circle5 For example, if we were really building an IntSet , we'd split it across two files: circle5 intset.h – the class IntSet {...} definition circle5 intset.cpp – the implementation of each method circle5 Unfortunately, the data members still must be part of the class definition. circle5 Since any programmer using an IntSet must see that definition, those programmers know something about the implementation. circle5 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 circle5 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. circle5 The second problem can have very drastic consequences. circle5 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. circle5 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 Document Right Arrow Icon
11/8/2009 2 Interfaces Separating out the details 7 circle5 Question : How can you provide a class definition that carries no implementation details to the client programmer, yet still has interface information? circle5 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.

{[ snackBarMessage ]}

Page1 / 6

14+-+Interfaces+and+Invariants - ADTs Recall EECS 280...

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

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