inheritance polymorphism & dynamic types › ... › 2016-17 › f › 3311 › slides ›...

37
20a-1 © Gunnar Gotshalks Inheritance Polymorphism & Dynamic Types

Upload: others

Post on 06-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-1!© Gunnar Gotshalks!

Inheritance!Polymorphism & Dynamic Types!

Page 2: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-2!© Gunnar Gotshalks!

Inheritance Terminology!

•  Any class that inherits directly or indirectly from C, including C itself is a descendant of C!

•  A proper descendant of C is a descendant of C other than itself!

C! ...!

Page 3: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-3!© Gunnar Gotshalks!

Inheritance Terminology – 2!

•  An ancestor of C is a class A such that C is! ! ! !descendant of A

!

•  A proper ancestor of C is an ancestor of C other than itself.!

C! ...!

Page 4: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-4!© Gunnar Gotshalks!

Subtyping Inheritance!

•  Subtyping relationship!»  Occurs when there is a strong degree of commonality

between two or more classes!>  E.g. between PERSON and EMPLOYEE!

Page 5: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-5!© Gunnar Gotshalks!

Subtyping Inheritance – 2!

•  Subtyping relationship!

»  Occurs when there is a strong degree of commonality between two or more classes!

>  E.g. between PERSON and EMPLOYEE!

•  An EMPLOYEE is a PERSON!»  employees behave like persons but also have their own

specialized behaviour!

Page 6: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-6!© Gunnar Gotshalks!

Subtyping Inheritance – 3!

•  Subtyping relationship!

»  Occurs when there is a strong degree of commonality between two or more classes!

>  E.g. between PERSON and EMPLOYEE!

•  An EMPLOYEE is a PERSON!

»  employees behave like persons but also have their own specialized behaviour!

•  When this degree of common behaviour occurs, EMPLOYEE is said to be a subtype of PERSON!

Page 7: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-7!© Gunnar Gotshalks!

Subtyping Inheritance – 4!

•  Subtyping relationship!

»  Occurs when there is a strong degree of commonality between two or more classes!

>  E.g. between PERSON and EMPLOYEE!

•  An EMPLOYEE is a PERSON!

»  employees behave like persons but also have their own specialized behaviour!

•  When this degree of common behaviour occurs, EMPLOYEE is said to be a subtype of PERSON!

•  Subtyping models the is-a relationship between classes!

Page 8: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-8!© Gunnar Gotshalks!

Dynamic Binding!

PERSON!

EMPLOYEE!

p1!

e1!

create p1 -- 1!!!create e1 -- 2!

PERSON!

EMPLOYEE!

p1!

e1!p1 := e1 -- 3!

Page 9: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-9!© Gunnar Gotshalks!

Dynamic Binding – 2!

•  It is the essence of polymorphism – multiple types!

Page 10: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-10!© Gunnar Gotshalks!

Dynamic Binding – 3!

•  It is the essence of polymorphism – multiple types!»  Ability to invoke methods applicable to the dynamic

type of an object rather than its static type!

Page 11: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-11!© Gunnar Gotshalks!

Dynamic Binding – 4!

•  It is the essence of polymorphism – multiple types!»  Ability to invoke methods applicable to the dynamic

type of an object rather than its static type!>  During execution we can attach a reference to

objects of different types!

Page 12: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-12!© Gunnar Gotshalks!

Dynamic Binding – 5!

•  It is the essence of polymorphism – multiple types!»  Ability to invoke methods applicable to the dynamic

type of an object rather than its static type!>  During execution we can attach a reference to

objects of different types!>  both PERSON and EMPLOYEE have a feature display

(EMPLOYEE inherits from PERSON)!

p1 , p2 : PERSON!e : EMPLOYEE!p2 := p1 -- ok type match !p1.display -- PERSON display!p1 := e -- ok, type conforms!p1.display -- EMPLOYEE display!

Page 13: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-13!© Gunnar Gotshalks!

Example hierarchy!

FIGURE *

OPEN_ FIGURE

* CLOSED_ FIGURE

*

SEGMENT POLYLINE POLYGON ELLIPSE

CIRCLE

RECTANGLE TRIANGLE

SQUARE

extent* barycenter* …

display* rotate* …

perimeter*

perimeter+ perimeter+

perimeter++ diagonal

... ...

perimeter++ perimeter++

•  Consider the following class hierarchy!

P468 Meyer!

Page 14: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-14!© Gunnar Gotshalks!

Figure Polymorphism!

•  Consider a figure hierarchy similar to that on the previous slide!

•  Suppose we had an array of figures and want to rotate all the figures in an array of figures!

>  Each figure has its own rotation method!

Page 15: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-15!© Gunnar Gotshalks!

Figure Polymorphism – 2!

•  Want a general and maintainable solution!

Page 16: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-16!© Gunnar Gotshalks!

Figure Polymorphism – 3!

•  Want a general and maintainable solution!

•  Want to be able to add new kinds of figures without !»  breaking previous programs!»  without modifying the rotate all figures method!

Page 17: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-17!© Gunnar Gotshalks!

Figure Polymorphism – 4!

•  Want a general and maintainable solution!

•  Want to be able to add new kinds of figures without !»  breaking previous programs!»  without modifying the rotate all figures method!

•  Solution!»  dynamic binding!

Page 18: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-18!© Gunnar Gotshalks!

Figure Polymorphism – 3!

-- In a parent class!!f : ARRAY [ FIGURE ]!!rotate_all ( d : real )! require d > 0! do! from i := f.lower! until i > f.upper! loop! f.item(i).rotate(d) -- dynamic binding! i := i + 1! end! end!

Page 19: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-19!© Gunnar Gotshalks!

Feature Call Rule!

!

In a feature call x.f where the type of x is based on a class C, feature f must be defined in one of the ancestors of C!

Page 20: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-20!© Gunnar Gotshalks!

Feature Call Rule – 2!

!»  Example in rotate_all!

>  rotate must be a feature in the class Figure!>  Each type of figure creates a custom instance of the

feature rotate !

In a feature call x.f where the type of x is based on a class C, feature f must be defined in one of the ancestors of C!

Page 21: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-21!© Gunnar Gotshalks!

Type Conformance Definition!

»  A type U conforms to a type T only if the declared class of U is a descendant of the declared class T!

Page 22: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-22!© Gunnar Gotshalks!

Type Conformance Definition – 2!

»  A type U conforms to a type T only if the declared class of U is a descendant of the declared class T!

»  For generically derived types, every actual parameter of U must (recursively) conform to the corresponding formal parameter in T!

>  void does not conform to expanded types!

Page 23: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-23!© Gunnar Gotshalks!

Type Conformance Rule!

! !!

An attachment of target x and source y is only valid if the type of y conforms to the type of x!

Page 24: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-24!© Gunnar Gotshalks!

Type Conformance Rule – 2!

! ! Attachment is either! x := y!or! y is an actual argument to parameter x!

An attachment of target x and source y is only valid if the type of y conforms to the type of x!

Page 25: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-25!© Gunnar Gotshalks!

Direct Instances & Instances!

»  A direct instance of a class C is an object produced according to the exact definition of C , either!

through a creation instruction, create x , where the target x is of type C !

or! recursively by cloning a direct instance of C!

»  An instance of C is a direct instance of a descendant of C!

Page 26: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-26!© Gunnar Gotshalks!

Static & Dynamic Types!

•  Static-dynamic type consistency!»  An entity declared of type T may, at run time only,

become attached to instances of T!

Page 27: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-27!© Gunnar Gotshalks!

Static & Dynamic Types – 2!

•  Static-dynamic type consistency!

»  An entity declared of type T may, at run time only, become attached to instances of T!

•  Static type is the type of the variable declared in the program text!

Page 28: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-28!© Gunnar Gotshalks!

Static & Dynamic Types – 3!

•  Static-dynamic type consistency!

»  An entity declared of type T may, at run time only, become attached to instances of T!

•  Static type is the type of the variable declared in the program text!

•  Dynamic type is the type of the instance attached at execution time !

Page 29: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-29!© Gunnar Gotshalks!

Static & Dynamic Types – 4!

•  Static-dynamic type consistency!

»  An entity declared of type T may, at run time only, become attached to instances of T!

•  Static type is the type of the variable declared in the program text!

•  Dynamic type is the type of the instance attached at execution time !

•  The type of void is NONE!

Page 30: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-30!© Gunnar Gotshalks!

Assignment Attempt!

•  Type rules ensure statically verifiable dynamic behaviour!»  No surprises at run time!

Page 31: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-31!© Gunnar Gotshalks!

Assignment Attempt – 2!

•  Type rules ensure statically verifiable dynamic behaviour!

»  No surprises at run time!

•  But type rules are too restrictive, consider!figlist : LIST [ FIGURE ]!

» What is the max diagonal of rectangles in the list?!figure := rectangle ; figure.diagonal ! Wrong!

Page 32: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-32!© Gunnar Gotshalks!

Assignment Attempt – 3!

•  Type rules ensure statically verifiable dynamic behaviour!

»  No surprises at run time!

•  But type rules are too restrictive, consider!figlist : LIST [ FIGURE ]!

» What is the max diagonal of rectangles in the list?!figure := rectangle ; figure.diagonal !

»  Cannot resolve as diagonal is not a feature of FIGURE!>  Do not want to have diagonal as a part of FIGURE as

all figures would need to define it!

Wrong!

Page 33: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-33!© Gunnar Gotshalks!

Assignment Attempt – 4!

•  We need to be able, in some circumstances, to know the dynamic type of an object!

Page 34: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-34!© Gunnar Gotshalks!

Assignment Attempt – 5!

•  We need to be able, in some circumstances, to know the dynamic type of an object!

•  Assignment attempt makes the assignment if the dynamic and static types conform, otherwise it returns void!

Page 35: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-35!© Gunnar Gotshalks!

Assignment Attempt – 6!

•  We need to be able, in some circumstances, to know the dynamic type of an object!

•  Assignment attempt makes the assignment if the dynamic and static types conform, otherwise it returns void!

if attached {SUBTYPE} supertype_object as subtype_object! then!

!Use subtype_object here! end!!

Page 36: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-36!© Gunnar Gotshalks!

Assignment Attempt Example!

maxdiag ( figlist : LIST [ FIGURE ] ) : REAL! require list_exists: figlist /= Void! do! from figlist.start ; Result := 0.0! until figlist.after! loop! if attached {RECTANGLE} figlist.item as r then! Result := Result.max ( r.diagonal )! end! figlist.forth! end! end!

Attempt assignment!

Use when successful!

Page 37: Inheritance Polymorphism & Dynamic Types › ... › 2016-17 › F › 3311 › slides › 20a-PolyA… · Dynamic Binding – 5! • It is the essence of polymorphism – multiple

20a-37!© Gunnar Gotshalks!

Polymorphic Creation!

•  Assume x is of static type T but we want to assign to x an instance of static type U where U is a descendant of T!

T!

U!

...!x!

Use!!create {U} x.make(…)!!!Obsolescent use of !!!! U ! x . make ( ... )!