12-subtypes - Last Time: * Alternate implementations *...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 Last Time: * Alternate implementations * Public interface is exactly the same Today: * Subtypes: substitution principle * Three properties that preserve substitutability * Subclasses: implementing subtypes * Beware: not all subclasses are subtypes * Apparent vs. Actual types We've talked a lot about types in class. Today, we'll add a new notion to our understanding of types: the "subtype". Suppose we have two types, S and T. We say that "S is a subtype of T", written "S <: T" if, for any instance where an object of type T is expected, an object of type S can be supplied without changing the correctness of the original computation. or If "S is a subtype of T" then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program (correctness) This is called the "substitution principle". It was first described in a paper written by Barbara Liskov and Jeannette Wing. Prof. Liskov is a chaired Professor at MIT. Prof. Wing was a student of Prof. Liskov's, and is currently the Chair of the CS department at CMU, and will soon be the director of Computer Science research at the National Science Foundation. Two notes. First, if S <: T then we also say that "T is a supertype of S". Second, supertypes are NOT substitutable for subtypes. All apples are fruits, but not all fruits are apples. Subtypes are different from the notion of "type-convertible". For example, in any computation that expects a double, you can use an int. However, the object isn't an int when it is used---it is first converted to a double---it's physical representation changes. However, if you use a subtype where a supertype is expected, it is not converted to the supertype---instead, it is used as-is. You have seen and worked with subtypes and supertypes: istream ifstream Looking at the following function:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sheet1 Page 2 void add(istream &source) { int n1, n2 source >> n1 >> n2 cout << n1+n2 } And consider two calls to the function 'add', one call with 'cin' and the other call with inFile that has been declared as ifstream inFile Also assume that inFile has been opened. add(cin) add(inFile) Both calls are valid -- and work. This is because ifstream is a subtype of istream ifstream can be supplied (substituted for istream) without changing the correctness. ************************************************************** In an ADT, there are three ways to create a subtype from a supertype: 1: Weaken the precondition of one or more operations. 2: Strengthen the postcondition of one or more operations. 3: Add one or more operations. The preconditions of a method are formed by its argument type signature plus it's REQUIRES clause. You weaken a REQUIRES clause by removing constraints on the caller---for example, consider the method int Foo::Bar(int baz) // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz).
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 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 20

12-subtypes - Last Time: * Alternate implementations *...

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