Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subclasses and Subtypes CMPS 2143. Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.

Similar presentations


Presentation on theme: "Subclasses and Subtypes CMPS 2143. Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning."— Presentation transcript:

1 Subclasses and Subtypes CMPS 2143

2 Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning or purpose of the child class A class is a subtype if the child class maintains the property of the principle of substitution. 2

3 Substitutability Feature of statically typed OO languages is that the type associated with the value held by a variable may not exactly match ▫ Examples:  GraphicalObject g = new Ball (…);  Room room = new King (…);  Person p = new Buddy (…); 3

4 Types Variable has a value that is a member of a set of values The name of the type is a handle we use to describe the set ▫ eg. 17 is an int We know more!!! ▫ Set of operations ▫ Set of properties (eg. int + int  int So a type has values, operations, properties… 4

5 Abstract Data Types (ADTs) Example: Stack ▫ Starting place to describe a stack is the interface – list all the operations ▫ Next – assign meaning in implementation  retrieve most recently added value (LIFO)  One could incorrectly implement the stack to satisfy the interface, but not the properties ▫ So the interface does NOT specify the properties and the implementation may not either 5

6 Subtype vs subclass A new class is a subtype if it provides all the operations of the parent class AND satisfies the properties associated with the parent class. ▫ Of course the child class is free to add new operations Subtype relationship is described purely in terms of behavior ▫ Says nothing about how new class defined or constructed 6

7 So a subtype is a subclass, BUT a subclass is not necessarily a subtype. Can also have a subtype that is not a subclass ▫ Dictionary is a subtype of Array – interface look the same, but Dictionary does not extend Array 7

8 Substitutability Paradox 1.A child class inherits all data fields defined in the parent. 2.A child class must recognize all behaviors associated with the parent (inherit directly or override) 3.Therefore, an instance of a child class can be used in any situation where an instance of a parent class is expected, with no observable difference. WEAK LINK in STEP 2! 8

9 Cont. Simply asserting the child class satisfies the interface common to the parent class does not ensure it will satisfy any of the properties of the parent. ▫ Example: Could inherit from Stack class, and violate the LIFO property. Language/Compiler CANNOT check this. ▫ Can ensure no inherited method is deleted (no mechanism to do this) ▫ Can ensure member data that is public is not made private ▫ BUT THAT’s IT. 9

10 Is this a problem? What would it take to create a subclass that is not also a subtype? ▫ Redefine an inherited operation while comprising some property without violating the type signature ▫ So yes, it can be done ▫ BUT, not commonly 10

11 Subclassing for construction A common situation where subclasses are created that are not subtypes is when inheritance is used purely for reusing code. Example ▫ class Dictionary : List {…}; ▫ Dictionary d = new Dictionary (..); ▫ Can determine how many elements are in a dictionary, because you could call the list method size d.size() 11

12 Cont. But now all the list methods can be used with this new class, and some may not be appropriate 12

13 Dynamically typed languages Subclasses vs subtypes (and types in general) used differently The set of operations that a variable of a certain type must recognize is termed its protocol. Any value passed as an argument to one its methods must usable within the method ▫ Example (C++ not dynamically typed, but templates close) template public T compareTo (T arg1, T arg2) { if arg1 < arg 2 then …. ▫ < has to be defined for T 13

14 Pre- and Postconditions Traditional way to specify behavior of ADT Precondition – states what must be true before a method can be executed Postcondition – states what is guaranteed to be true after the method is executed (as long as precondition was met) We can informally do this in comments We can use assertions (assert statement in C++) Eiffel more formal about it ▫ Child class CANNOT override pre- and post-conditions 14

15 Example: //precond: Stack s is not full //postcond: item is inserted at top of s, and s is // unchanged otherwise public void push (ItemType item); 15

16 Study questions – page 219 1-7, 9, 12 16


Download ppt "Subclasses and Subtypes CMPS 2143. Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning."

Similar presentations


Ads by Google