13+-+Subtypes+and+Subclasses

13+-+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
11/1/2009 1 Subtypes and Subclasses EECS 280 Programming and Introductory Data Structures 1 Subtypes Recall – Introduction c Suppose we have two types, S and T. c S is a subtype of T, written “S <: T”, if: c 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> c Objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program (correctness). c This is called the “substitution principle”. c If S <: T, then we also say that “T is a supertype of S”. 2 Subtypes Creating 3 c 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 c The preconditions of a method are formed by two things: c It’s argument type signature c The REQUIRES clause int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). c You could weaken this REQUIRES clause by allowing: c Negative, even integers c Positive integers c All integers Subtypes Creating 5 int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). c You weaken the argument type signature by allowing supertypes of specific arguments, or by making some arguments optional . c We'll see examples of optional arguments in the semester, but we won’t see many instances of weakening through supertypes. Subtypes Creating 6 c The postconditions of a method are formed by two things: c It’s return type signature c The EFFECTS clause. c 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 Document Right Arrow Icon
11/1/2009 2 Subtypes Creating 7 int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). c 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) . c Something expecting bar(baz) would not be inconvenienced by that message. c 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 c The final way of creating a subtype is to add some new method to the subtype. c Any code expecting only the "old" methods will still see all of them. c The "new" method makes no difference. c 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 C++ has a mechanism to enable subtyping, called "subclassing", or sometimes "inheritance". 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.

{[ snackBarMessage ]}

Page1 / 7

13+-+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