160 likes | 289 Views
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 or purpose of the child class A class is a subtype if the child class maintains the property of the principle of substitution.
E N D
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 or purpose of the child class • A class is a subtype if the child class maintains the property of the principle of substitution.
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 (…);
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…
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
Subtype vs subclass • A new class is a subtype if it provides all the operations of the parent class ANDsatisfies 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
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
Substitutability Paradox • A child class inherits all data fields defined in the parent. • A child class must recognize all behaviors associated with the parent (inherit directly or override) • 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!
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.
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
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 <Association> {…}; • Dictionary d = new Dictionary (..); • Can determine how many elements are in a dictionary, because you could call the list method size d.size()
Cont. • But now all the list methods can be used with this new class, and some may not be appropriate
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 <class T> public T compareTo (T arg1, T arg2) { if arg1 < arg 2 then …. • < has to be defined for T
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
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);
Study questions – page 219 • 1-7, 9, 12