5. subtyping and inheritance - ¢â‚¬¢ inheritance...

Download 5. Subtyping and Inheritance - ¢â‚¬¢ Inheritance ¢â‚¬¢ Object-oriented type systems ¢â‚¬¢ Behavioral subtyping

Post on 07-Jul-2020

4 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • 20.01.2006 130© A. Poetzsch-Heffter, TU Kaiserslautern

    5. Subtyping and Inheritance Overview:

    • Core concepts

    • Subtyping and dynamic binding

    • Inheritance

    • Object-oriented type systems

    • Behavioral subtyping

    Remark:

    5.1 Core Concepts

    Object-orientation has three core concepts :

    1. Object model

    2. Object interfaces

    3. „is-a“ relation between objects enabling

    - polymorphism and classification

    - abstraction and specialization

    Notice that the object model is a prerequisite to object interfaces which in turn are the basis for t he „is-a“ relation.

  • 20.01.2006 131© A. Poetzsch-Heffter, TU Kaiserslautern

    We can say that an object/item/term of type A is also an object/item/term of type B iff A-objects have all relevant properties of B-objects.

    „is-a“ relations can be used to classify objects/items/ terms into hierarchical structures according to the ir properties. The result is often called a classifica tion.

    The objects/items/terms higher up in the classifica tion are called more general /abstract than those below them which in turn are more special /concrete than the former.

    Explanation: („is-a“ relation, classification)

    Example: (classification) 1. Classification of vertebrates:

    Vertebrate

    Fish Amphibian Reptile BirdMammal

    Whale ArtiodactylPrimate …

  • 20.01.2006 132© A. Poetzsch-Heffter, TU Kaiserslautern

    2. Classification of figures:

    Figure

    Ellipse Polygon

    Circle TriangleQuadrangle

    Parallelogram

    RectangleLozenge

    Square

    Arrows represent the „is-a“ relation

    Goal: Apply classification to software artefacts!

  • 20.01.2006 133© A. Poetzsch-Heffter, TU Kaiserslautern

    Classification in Object-Oriented Programming:

    • Syntactic classification:

    Subtype objects understand at least the messages that supertype objects understand and have at least the same attributes ( wider interface).

    • Semantic classification:

    Subtype objects provide at least the behavior of supertype objects (behavioral subtyping).

    • Substitution principle:

    Subtype objects can be used where objects of their supertypes are expected.

    Polymorphism is the quality of being able to assume different forms.

    A program part is polymorphic if it can be used for objects of different types.

    Subtyping is a special kind of polymorphism in which types are ordered w.r.t. inclusion relation: Object s of a subtype belong as well to the supertype.

    Explanation: (subtype polymorphism)

  • 20.01.2006 134© A. Poetzsch-Heffter, TU Kaiserslautern

    Other kinds of polymorphism:

    – Parametric polymorphism (generic types)

    – Ad-hoc polymorphism (method overloading)

    Remark:

    Abstraction means to form a general concept by extracting common features from specific examples.

    Explanation: (abstraction)

    Designing Classifications:

    Abstraction in OO-programming:

    • Start from different objects or types with common properties

    • Develop a more abstract type, extracting the common properties

    • Corresponds to making the interface smaller

    • Program parts that only rely on the common properties work for all objects of the more abstrac t type

  • 20.01.2006 135© A. Poetzsch-Heffter, TU Kaiserslautern

    Person

    ProfessorStudent B ot

    to m

    -u p

    Example: (abstraction)

    class Student { String name; int regno; …

    void print( ) { System.out.println(name); System.out.println(regno);

    } }

    class Professor { String name; String room; …

    void print( ) { System.out.println(name); System.out.println(room);

    } }

    interface Person { void print( );

    }

    class Student implements Person { … }

    class Professor implements Person { …}

    Abstraction

  • 20.01.2006 136© A. Poetzsch-Heffter, TU Kaiserslautern

    Person[ ] p = new Person[4]; p[0] = new Student (…); p[1] = new Professor (…); … for ( int i=0; i < 4; i++ )

    p[ i ].print( );

    Application of abstraction, that is, of type Person :

    „Algorithm” based on Person

    Specialization means to add specific properties to an object or to refine a concept by further charact e- ristics.

    Explanation: (specialization)

    Specialization in OO-programming:

    • Start from general objects or types.

    • Extend them and their implementations.

    • Requirement: Behavior of specialized objects shoul d conform to behavior of more general objects.

    • Program parts that work for the more general objec ts work as well for specialized objects.

    • Implementation parts can be reused (inheritance).

  • 20.01.2006 137© A. Poetzsch-Heffter, TU Kaiserslautern

    Person

    ProfessorStudent

    T op-dow

    n

    Example: (specialization)

    Specialization:

    – Develop implementa- tion for type Person

    – Specialize it

    – Inheritance and over- riding can be used

    class Person { String name;

    … void print( ) { System.out.println( name );

    } }

    class Student extends Person {

    int regno; … void print( ) {

    super .print( ); System.out.println(regno);

    } }

    class Professor extends Person {

    String room; …

    void print( ) { super .print( ); System.out.println(room);

    } }

  • 20.01.2006 138© A. Poetzsch-Heffter, TU Kaiserslautern

    5.2 Subtyping & Dynamic Binding

    In a type system with subtyping , the types are partially ordered. If S ≤ T, we say that S is a subtype of T and T a supertype of S.

    Elements of the subtype belong as well to the supertype, i.e. types are not disjoint.

    Explanation: (subtyping)

    To enable classification of objects, all typed OO-languages support subtyping (there are as well other languages supporting subtyping).

    Example: (type system with subtyping)

    Types:

    • primitive datatypes: int, char, byte, ....

    • Interface types

    • Class types

    • Array types

    Reference types

  • 20.01.2006 139© A. Poetzsch-Heffter, TU Kaiserslautern

    Subtype relation:

    Declaration: interface S extends T1, T2, ... implicit: S

  • 20.01.2006 140© A. Poetzsch-Heffter, TU Kaiserslautern

    Let S be a subtype of T. Let TV be a type occurrence in T (attribute-, parameter-, result type) and SV the corresponding occurrence in S. We say:

    - SV and TV are covariant if SV is a subtype of TV.

    - SV and TV are contravariant if TV is a subtype of SV.

    Explanation: (Co-/contravariance)

    OO-languages can only be statically typed iff:

    - Attribute types are co- and contravariant, i.e. invariant.

    - Parameter types are contravariant.

    - Result types are covariant.

    Fact:

    Example: (Co-/contravariance)

    interface C1 { String a; ... }

    class D1 implements C1 { Object a; ... }

    ...

    C1 cvar = new D1(); // initializes a

    String svar = cvar.a;

    // Type error: a not covariant in D1

    1. Attributes have to be covariant:

    Assuming that overriding of attributes is allowed:

  • 20.01.2006 141© A. Poetzsch-Heffter, TU Kaiserslautern

    interface C2 { Object a; ... }

    class D2 implements C2 { String a; ... }

    ...

    C2 cvar = new D2();

    cvar.a = new Object();

    // Type error: a not contravariant in D2

    2. Attributes have to be contravariant:

    3. Parameter types have to be contravariant:

    interface C3 { int m(Object p); ... }

    class D3 implements C3 {

    int m(String s){ s.length();...}

    } ...

    C3 cvar = new D3();

    cvar.m( new Object() );

    // Type error when executing m:

    // parameter not contravariant

  • 20.01.2006 142© A. Poetzsch-Heffter, TU Kaiserslautern

    Covariance must as well hold for exception types.

    interface C4 { String m(); ... }

    class D4 implements C4 { Object m(){...} ...}

    ...

    C4 cvar = new D4();

    String svar = cvar.m();

    // Type error: result type not covariant

    4. Result types have to be covariant:

    Remark:

    • Co- and contravariance are important to understand object-oriented typing and the principles underlyin g behavioral subtyping.

    • Java has a fairly inflexible type system:

    - only restricted contravariance for parameters

    - up to 1.4 no covariance for result types

  • 20.01.2006 143© A. Poetzsch-Heffter, TU Kaiserslautern

    Subtyping together with dynamic binding can express classification. Inheritance is not needed:

    Every class hierarchy can be expressed by a hierachy of types where only the minimal types have an implementation.

    Expressiveness of Subtyping:

    Example: (refactoring class hierarchies)

    A

    B C

    D

    A

    B C

    D

    AImpl

    CImpl

    DImpl

    BImpl

  • 20.01.2006 144© A. Poetzsch-Heffter, TU Kaiserslautern

    5.3 Inheritance

    Inheritance is a language concept to support the implementation of subclasses. Subclasses are implemented by:

    - inheriti

Recommended

View more >