1 / 16

Subclasses and Subtypes

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.

ting
Download Presentation

Subclasses and Subtypes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.

  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 (…);

  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…

  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

  6. 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

  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

  8. 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!

  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.

  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

  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 <Association> {…}; • Dictionary d = new Dictionary (..); • Can determine how many elements are in a dictionary, because you could call the list method size d.size()

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

  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 <class T> public T compareTo (T arg1, T arg2) { if arg1 < arg 2 then …. • < has to be defined for T

  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

  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);

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

More Related