15+-+Subtypes+and+Subclasses

15+-+Subtypes+and+Subclasses - Subtypes Recall Introduction...

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

View Full Document Right Arrow Icon
9/1/10 1 Subtypes and Subclasses EECS 280 Programming and Introductory Data Structures 1 Subtypes Recall – Introduction Suppose we have two types, S and T. 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> 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”. If S <: T, then we also say that “T is a supertype of S”. 2 Subtypes Creating 3 In an Abstract Data Type, 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. Subtypes Creating 4 The preconditions of a method are formed by two things: It’s argument type signature The REQUIRES clause int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). You could weaken this REQUIRES clause by allowing: Negative, even integers Positive integers All integers Subtypes Creating 5 int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). You weaken the argument type signature by allowing supertypes of specific arguments, or by making some arguments optional . We'll see examples of optional arguments in the semester, but we won’t see many instances of weakening through supertypes. Subtypes Creating 6 The postconditions of a method are formed by two things: It’s return type signature The EFFECTS clause. You strengthen the EFFECTS clause by promising everything you used to, plus extra.
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 Subtypes Creating 7 int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). We could strengthen the effects clause of Foo::Bar() by printing a message to the screen for each invocation, in addition to computing bar(baz) . Something expecting bar(baz) would not be inconvenienced by that message. You strengthen the return type by promising to return a subtype instead. We won't see many instances of strengthened return types in this course. Subtypes Creating 8 The final way of creating a subtype is to add some new method to the subtype. Any code expecting only the "old" methods will still see all of them. The "new" method makes no difference. As an example, we'll create a subtype of IntSet , called MaxIntSet , by adding an operation that returns the maximum element in the set. Subclasses Creating Subclasses using inheritance 9 C++ has a mechanism to enable subtyping, called "subclassing", or sometimes "inheritance".
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 / 7

15+-+Subtypes+and+Subclasses - Subtypes Recall Introduction...

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