InheritanceExample : Rectangle is a Polygon.Slide 3Example: OrderedList is a List.ImplementationsSlide 6OrderedList is not a subtype of List.PolymorphismSlide 9Typing for InheritanceSlide 11Static and Dynamic TypeStatic-dynamic type consistencySlide 14Slide 15Type Casting : Assignment AttemptDynamic BindingDynamic Binding and EfficiencyC++ Approach : What not to do!C# Approach(cont’d)The meaning of Inheritance : Q is a P.Quick Recap of Applications of InheritanceInter-Class RelationshipsSubclass instance ; Client fieldChoosing between Client and InheritanceCoupling : Inheritance vs Compositionpublic UpdatesDelegation and CompositionInterface InheritanceSoftware ICs (Brad Cox)Slide 32Example : OOP Style vs Procedural StyleClient in SchemeSuppliers and Client in JavaDynamic Binding : RevisitedForms of InheritanceAccess Control in C++Interpretation of privateSlide 40Slide 41Interaction between Overloading and Overridingceg860 (Prasad) L112Inh 1Inheritance Reusability Reusability ; Extendibility Extendibility ; Type Safety Type Safety(Minimize Repetition, Accommodate Variation in a Reliable way)ceg860 (Prasad) L112Inh 2Example : Rectangle is ais a Polygon.•Polygon ( (Direct) Ancestor )Methods: translate, rotate, perimeter, etcFields: listOfVertices, etc•Rectangle inherits frominherits from Polygon.Methods: translate, rotate, etcFields: listOfVertices, etc•Rectangle ( (Direct) Descendant )Changes Method (for Efficiency only) : perimeterAdds Field: diagonalceg860 (Prasad) L112Inh 3•Invariant Inheritance Rule–The invariant of a class applies to all its descendants. That is, the class invariant of all ancestors applies to a class.•Constructors–A constructor establishes the class invariant.–So, a parent’s constructor cannot serve as the child’s constructor, in general. That is, constructors are not inherited as constructors. –However, a parent’s constructor is accessible in the child class.ceg860 (Prasad) L112Inh 4Example: OrderedList is ais a List. •List –Methods: empty(), isEmpty(), insert(_), head(), tail(). •OrderedList – New constructor: order(List)–“Inherited” Methods : empty(), isEmpty(), insert(_), head(), tail():Modified Semantics•head() : Returns the smallest element,•tail() : Deletes all occurrences of the smallest element.ceg860 (Prasad) L112Inh 5Implementations •Invariant 1 The array representation is sorted in the non-decreasing order.–Code shared : empty(), isEmpty(), head()–Redefined : tail(), insert(_) •Invariant 2 true–Code shared : empty(), isEmpty(), insert(_)–Redefined : head(), tail()ceg860 (Prasad) L112Inh 6•Behavioral specification ADT OrderedList is not a subtypesubtype of ADT List.•List: length(tail(L)) + 1 = length(L)•OrderedList: length(tail(L)) + 1 <= length(L)•Implementation –Class OrderedList is a subclasssubclass of class List.•Code sharing.•Notion of Substituitivity–Java uses message signatures.–Eiffel encourages behavioral view.ceg860 (Prasad) L112Inh 7OrderedList is not a subtype of List.List li = new List(); li.insert(5);li.insert(5);li.insert(5);li.tail();li.tail();// assert:// ! li.isEmpty()List li = new OrderedList(); li.insert(5);li.insert(5);li.insert(5);li.tail();li.tail();// exception:// NullPointerceg860 (Prasad) L112Inh 8Polymorphism Polygon p; Rectangle r; Triangle t; p := r; p := t; r := p;•Polymorphic entity (reference type)•Descendants•Polymorphic assignment•type of rhs conformsconforms to type of lhs•Illegal assignmentceg860 (Prasad) L112Inh 9Polymorphic Assignment p := t;pt1. No transmutation of an object.2. No change in (run-time) type of the object.3. No change in (compile-time) type of the entities.4. Only reference reattachment.5. Class (type) of t is a descendant of class(type) of p.ceg860 (Prasad) L112Inh 10Typing for Inheritance•Goal–Guarantee at compile-time that no incorrect run-time type combination can occur. •Feature Call rule–In x.f, where type of x is based on C, feature f must be defined in one of the ancestors of C.•legal : p.rotate(), r.rotate(), p.perimeter(), r.perimeter(), etc•illegal: p.diagonalceg860 (Prasad) L112Inh 11•Conformance A type U conformsconforms to type T if and only if the class of U is a descendant of the class of T.•Type Conformance Rule An attachment of target x and source y (in an assignment x := y; or in the use of y as an actual argument to a routine call where x is the formal argument) is only valid if the type of y conforms to the type of x.•Illegal: t := r; p := t; t:= p; •Illegal: p:= r; p.diagonal;ceg860 (Prasad) L112Inh 12Static and Dynamic Type Dynamic typeAn object only has a dynamic type that never changes during the object’s lifetime. (RTTI)At any time during the execution, a reference/entity has a dynamic type which is the type of the object it is attached to. Static typeOnly an entity has a static type, the type with which it was declared.ceg860 (Prasad) L112Inh 13Static-dynamic type consistencyAn entity declared of type T may at run-time only become attached to instances of class T. Instances of class T include direct instances of class T and instances of the descendants of class T.ceg860 (Prasad) L112Inh 14•Statically typed language (E.g., Ada) static-type(entity) = dynamic-type(object)– All type checking done at compile-time–Efficient but inflexible.– OOPL Cases: Java’s primitive types, Eiffel’s expanded types, etc.•Dynamically typed language (E.g., Scheme) Entity type-less; objects carry type-tags.–All type checking done at run-time (inefficient).–Flexible but inefficient.–OOPL E.g.: Smalltalk.Pceg860 (Prasad) L112Inh 15•Strongly typed OOPLs (E.g., Eiffel, Java, C++) dynamic-type(object) conforms to conforms to static-type(entity) –Flexible Supports heterogeneous data structures (through dynamic binding) –Reliable Provides compile-time checks to ensure that there are no undetected type violations at run-time.ceg860 (Prasad) L112Inh 16Type Casting : Assignment Attempt p := r; … ; r := (Rectangle) p;•ApplicationsExternal : Testing objects received over the network, or retrieved from persistent storage.Internal :
View Full Document