Session9 Polymorphism

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Session 9 Polymorphism

Ad-Hoc Polymorphism Inclusion Polymorphism

Session 9 - Polymorphism

1/26

Objectives
Ad-Hoc Polymorphism
Types Ad-Hoc Polymorphism Universal Polymorphism

Inclusion Polymorphism
Function Bindings Polymorphic Objects

Session 9 - Polymorphism

2/26

Polymorphism
Polymorphism means many forms. In programming languages, polymorphism refers to the flexibility of associating different data types with objects and functions.
Polymorphic objects may have different data types. Polymorphic functions may accept arguments of different data type.

In OOP polymorphism finds its most powerful application in inheritance hierarchies


Session 9 - Polymorphism 3/26

Types (1)
A type describes how to interpret the bits stored in memory.

Session 9 - Polymorphism

4/26

Types(2)
The type describes the object's (or variable's) properties and the operations that may be performed upon the object (or variable). The type does not change throughout the lifetime of the variable or object.

Session 9 - Polymorphism

5/26

Static Type Checking


We use compilers to check for type consistency and to identify undefined operations. This is called static type checking. Static type checking applies to monomorphic objects (an object declared as an instance of a particular class).

Session 9 - Polymorphism

6/26

Dynamic Type Checking


Rather than declare the object as an instance of a particular class, we only declare a pointer to the object. We identify its type only when we allocate memory for the object
Human* h; if(){ h = new Student(); } else { h= new Professor (); }

Since the execution path is unknown at compile time, type checking occurs at run time. This is called dynamic type checking.
7/26

Session 9 - Polymorphism

Christopher Strachey
The notion of polymorphism as applied to functions dates back four decades. In 1967, Christopher Strachey distinguished
functions that work differently on different argument types from (adhoc). functions that work uniformly on a range of argument types (parametric).

Session 9 - Polymorphism

8/26

Cardelli and Wegner


In 1985, Cardelli and Wegner refined Strachey's distinction.

Session 9 - Polymorphism

9/26

Ad-Hoc Polymorphism
Ad-Hoc polymorphism refers to those cases where we use the same syntax with objects of different type (single function that can be called using different argument types).

Session 9 - Polymorphism

10/26

Ad-Hoc Polymorphism -Overloading Overloading redefines a function using the same name but a different signature. We write a distinct definition for each signature. Compilers implement overloading at compile time.

Session 9 - Polymorphism

11/26

Ad-Hoc Polymorphism -Coercion(1) Coercion: Automatic type conversions invoked by the compiler to change one type to another as a result of the compiler's attempts to match a function definition to the argument types in a function call. Coercion is implemented at compile time.

Session 9 - Polymorphism

12/26

Ad-Hoc Polymorphism -Coercion(2) Coercion may be further subdivided into


narrowing and widening (promotion)

conversions as well as
implicit and explicit (casting)

conversions.

Session 9 - Polymorphism

13/26

Universal Polymorphism
Universal polymorphism (true polymorphism) refers to a single function definition that applies to a variety of types

Session 9 - Polymorphism

14/26

Universal Polymorphism -Inclusion Implemented at run time. It refers to those cases where an object is identified with several types that are related by an inclusion relation (inheritance).

Session 9 - Polymorphism

15/26

Universal Polymorphism -Parametric Parametric (generic) polymorphism is implemented at compile time. It refers to the use of type parameters with functions.

Session 9 - Polymorphism

16/26

Inclusion Polymorphism
The classes that belong to an inheritance hierarchy exhibit inclusion relations amongst themselves. These relations enable us to associate an object with the inheritance hierarchy, but to postpone identification with a specific class until run time.

Session 9 - Polymorphism

17/26

Function Bindings
The compiler generates code that associates each function call in a program with a function definition. This code is called a function binding. 02 forms:
early binding or late binding.

Session 9 - Polymorphism

18/26

Early Binding
An early binding is a binding between a function call and a function definition that is defined at compile time. Demo..

Session 9 - Polymorphism

19/26

Late Binding
A late binding is a binding between a function call and a function definition that is defined at run time. To implement late binding, the compiler inserts a virtual table that holds the information needed to select the appropriate function at run time. Demo..
Session 9 - Polymorphism 20/26

Virtual Destructors
If a derived class allocates resources, it needs its own destructor to release those resources. The presence of a virtual base class destructor ensures that the most derived destructor will be called if and when a class is derived from the base class. Demo..

Session 9 - Polymorphism

21/26

Polymorphic Objects(1)
An object fully declared at compile time is monomorphic. Its type does not change during its lifetime. An object partly declared at compile time and only fully declared at run time may be polymorphic. Space for an object fully declared at run time is allocated on freestore.

Session 9 - Polymorphism

22/26

Polymorphic Objects(2)
C++ objects are monomorphic by default. To declare a polymorphic object, we define:
a pointer to the object, a reference to the object, a receive-by-reference parameter for the object, or a receive-by-address parameter for the object.

Session 9 - Polymorphism

23/26

Inclusion polymorphism Advantages


Inclusion polymorphism is extremely useful in minimizing the amount of code written. Sample code:
Human h[ ]={new Student(), new Professor(), new Employee()};

Session 9 - Polymorphism

24/26

In-Class Practice

Inclusion Polymorphism Handout

Session 9 - Polymorphism

25/26

Summary
Ad-Hoc Polymorphism
Types Ad-Hoc Polymorphism Universal Polymorphism

Inclusion Polymorphism
Function Bindings Polymorphic Objects

Q&A
Session 9 - Polymorphism 26/26

You might also like