14+-+Interfaces+and+Invariants

14+-+Interfaces+and+Invariants - 11/8/2009 ADTs Recall EECS...

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 c 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 c To the caller, an ADT is only an interface. c Here are two definitions: c Good Definition : The contract for using things of this type. c 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. c 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 c The class mechanism, as we've used it so far, has one shortcoming: c It co-mingles details of the implementation with the definition of the interface . c Recall that the implementation of a class includes: 1. Data members 2. Method implementations c The method implementations can be written separately from the class definition and usually are. Interfaces Separating out the details 5 c For example, if we were really building an IntSet , we'd split it across two files: c intset.h – the class IntSet {. ..} definition c intset.cpp – the implementation of each method c Unfortunately, the data members still must be part of the class definition. c Since any programmer using an IntSet must see that definition, those programmers know something about the implementation. c 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 c 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. c The second problem can have very drastic consequences. c 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. c 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
11/8/2009 2 Interfaces Separating out the details 7 c Question : How can you provide a class definition that carries no implementation details to the client programmer, yet still has interface information? c Remember, classes must contain their data members, so this means that the class cannot have an implementation. c
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 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 6

14+-+Interfaces+and+Invariants - 11/8/2009 ADTs Recall EECS...

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