1 / 37

Polymorphism

Polymorphism. Principles of Object-Oriented Software Development (Chapter 9). Objectives. Present a more systematic treatment of the concept of subtyping Examine more general notions of polymorphism Introduce methods for formal analysis of types, polymorphism and inheritance. Inheritance.

shaw
Download Presentation

Polymorphism

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. Polymorphism Principles of Object-Oriented Software Development (Chapter 9)

  2. Objectives • Present a more systematic treatment of the concept of subtyping • Examine more general notions of polymorphism • Introduce methods for formal analysis of types, polymorphism and inheritance

  3. Inheritance • Uses • During analysis: a technique for discovering taxonomies • During object design: an opportunity reusing code from existing classes • Inheritance during object design • Implementation inheritance • When the subclass and superclass relationship does not represent a taxonomy • Specification inheritance • When the subclass is also a subtype of the superclass • When is it safe to do inheritance?

  4. Liskov Substitution Principle • General principle: • If an object of type S can be substituted in all places where an object of type T is expected, then S is a subtype of T • Object-oriented interpretation: • A class S, which inherits from a superclass T, is considered a subtype of T if S can be substituted in all places where T is expected. • Advantage: new subclasses of T can be written without modifying the methods of T. • Specification inheritance follows the Liskov substitution principle. • How can we formally define this substitution principle?

  5. Outline • The subtype relation • Flavors of polymorphism • Type abstraction

  6. Abstract inheritance • In knowledge representation, inheritance hierarchies are used to represent “is-a” relationships • Abstract inheritance: creating declarative relationships • Declarative taxonomic hierarchies represent predicate logic assertions • These relationships may be non-monotonic • Monotonicity: all properties of the ancestor are preserved by the descendants • Knowledge representation is non-monotonic • “is-not” relationships in addition to “is-a” relationships • Example:

  7. The subtype relation • Subtypes satisfy the monotinicity property • Types can be represented as sets • Union of basic data types and compound types (records, variants, functions) • More formally: • Subtypes correspond to subsets • Subtypes should be compatible with supertypes • Subtype can be used anywhere supertype is used – conformance or substitutability property • Several refinement relationships preserve this property

  8. Subtype Refinement Relations • Sub-range inclusion • Functions • Records • Variants (contravariance)

  9. Sub-range Inclusion • If n  n’ and m m’ then the subrange n’..m’ is a subtype of n..m • Example: • 3..5  2..6

  10. Functions • If  is a subtype of  and  is a subtype of  then The function  is a subtype of the function  (Function notation:  means function taking parameter of type  and return value of type ) Subtype can have looser parameter type and/or tighter return type • Examples: float  float is not a subtype of float  int int  float is not a subtype of float  int int  int is a not subtype of float  int float  char is a subtype of float  int double  int is a subtype of float  int

  11. Contravariance Rule for Function Subtyping • Function subtypes can have looser parameter type and/or tighter return type • Intuitively, for a subtype function to be used in place of its supertype: • It must be able to support at least the same set of values as the input parameters of the supertype, and, • It must return no value more than what its supertype returns • A subtype function is a refinement of its supertype, relaxing the restrictions for its calling function, while strengthening its obligations to the calling function.

  12. Records • If type i is a subtype of i, for all i = 1..m, then The record defined by a1:1,…,an:n is a subtype of the record defined by a1:1,…,am:m Note that n could be greater than m  the subtype can introduce additional fields not in the supertype • Example: {age:int, speed:int, fuel:int}  {age:int, speed:int} • Locations that use the supertype are not aware of the new fields so they are never used in those contexts.

  13. Variants • Variants are also known as unions in C, C++ • If type i is a subtype of i, for all i = 1..m, then A variant [a1: 1 …  am:m] is a subtype of [a1:1  …  an:n], m  n Subtypes of a variant are created by removing one or more of the available variant fields

  14. Objects as Records • An easy way to understand when a subclass is also a subtype of its superclass is to treat objects as records (whose fields can be data or functions). • As a heuristic, methods and attributes of the superclass can be overridden as long as the replacement methods and attributes are subtypes of the originals. • Exception cases: data hiding, generics, self-references

  15. Object Subtyping Examples • type any = { } • type entity = { age : int } • type vehicle = { age : int, speed : int } • type machine = { age : int, fuel : string } • type car = { age : int, speed : int, fuel : string }

  16. Type Conformance • S provides at least the operations of T • for each operation in T, the corresponding operation in S has the same number of arguments • the type of the result of operations of S conform to those of the operations of T • the types of arguments of operations of T conform to those of the operations of S

  17. Outline • The subtype relation • Flavors of polymorphism • Inclusion • Parametric • Overloading • Type abstraction

  18. The Nature of Types • Typing – protection against errors • Static typing – type-checking at compile time • Strong typing – all expressions are type consistent • Untyped languages – flexibility • bitstrings, sets, -calculus • Exceptions to monomorphic typing: • overloading, coercion, subranging, value-sharing (nil)

  19. Polymorphism • The ability to process objects differently depending on their data type • Polymorphic function • A function that can evaluate to and be applied to values of different types. • Polymorphic datatype • A datatype that contains elements of different types. • Polymorphism is not restricted to object types only.

  20. Flavors of polymorphism Inclusion (inheritance) Universal Parametric (generics) Polymorphism Overloading (intersection) Ad-hoc Coercion • Inclusion polymorphism – to model inheritance • Parametric polymorphism – supports generics (templates) • Intersection types – overloading done systematically • Coercion – typecasting

  21. Inheritance – Incremental Modification • Inclusion polymorphism can be understood by regarding inheritance as a means to define the properties of a subtype incrementally, by adding information • Thus, we can characterize inheritance as R = P + M • Result = Parent + Modifier Example: R = {a1,a2} + {a2,a3} = {a1,a2,a3} • Independent attributes: M disjoint from P • Overlapping attributes: M overrules P • 3 types of attributes: • Redefined attributes – redefined by M • Virtual attributes – must be defined in M • Recursive attributes – defined in P

  22. Outline • The subtype relation • Flavors of polymorphism • Type abstraction • Subtype calculus • Intersection types • Bounded polymorphism • Existential types – hiding • Self-reference

  23. Type Abstraction • Purpose: to define the valid typing for the various forms of polymorphism, by expressing them in a syntactic way, using type expressions • Based on lambda calculus • Lambda terms • Variables • Abstraction • Application

  24. More on Lambda Calculus • Laws • Beta conversion (parameter passing) • Extensionality in application • Extensionality in abstraction

  25. More on Lambda Calculus • Substitution

  26. Type Calculi • Subtype calculus – models subtypes • Intersection type calculus – models overloading • Calculus for bounded polymorphism – models generics • Existential type calculus – models data hiding • Recursive type calculus – models inheritance

  27. Simple Type Calculus for Subtypes • Elements • Type assignment • Refinement A type  can be a basic type  or a function An expression e can be a variable, a typed function abstraction, or application The expression e has type , assuming that type assignments in  are valid

  28. Example applications • Determining subtypes • Since Int is a subtype of Double, D is a subtype of S • It can be shown that C++ does not fully support subtyping by providing a type expression that is valid in this calculus but its corresponding C++ program gets a typechecking error

  29. Intersection Type Calculus • Elements • Type assignment • Refinement

  30. Example Application • This gives us a way to express a limited form of overloading by enumerating all possible types • Defining + as an overloaded operator:

  31. Calculus for Bounded Polymorphism • Elements • Type assignment • Refinement

  32. Example Applications • Parametrized types • Specifying semantics for C++ templates

  33. Existential Type Calculus • Extend from calculus for bounded polymorphism • Elements • Type assignment • Refinement

  34. Existential Types and Data Hiding • Existential types enable modeling of abstract data types and hiding by means of packages and type abstraction • Hiding – expressed as existential types • Packages – expressing abstract data types

  35. Calculus for Recursive Types • Extend from calculus for bounded polymorphism • Elements • Type assignment • Refinement

  36. Self-References and Inheritance • Calculus employing recursive types • Recursive type – defined with itself as one of the components • Can be used to characterize the semantics of inheritance more precisely • Object semantics determined by unrolling the recursive type • Express the semantics of dynamic binding

  37. Summary • Subtype refinement relations determine what types are substitutable • There are many flavors of polymorphism of which inclusion polymorphism due to inheritance is one • Semantics of different flavors of polymorphism can be specified using type calculi derived from lambda calculus • Consistency of a programming language’s type system can be analyzed using these calculi

More Related