Professional Documents
Culture Documents
14 Polymorphism PDF
14 Polymorphism PDF
Polymorphism
Definition
1
14 - Polymorphism
Polymorphism in programming
languages
Polymorphism in untyped
languages
2
14 - Polymorphism
Forms of polymorphism
» polymorphic variables
» pure polymorphism (functions with
polymorphic arguments)
» overloading
» overriding
» abstract or deferred methods
Polymorphic variables
3
14 - Polymorphism
Overloading
» With overloading, it is the function name that is polymorphic.
» A single function name which is used to denote two or more
function bodies is said to be overloaded
» A common example in almost all languages, + means both
integer and floating addition
» Determination of which function to execute is made by
examining the arguments
» Depending upon language and form used, can either be made
at compile-time or run-time
» Should not be confused with overriding or refinement -
functions need not be related by classes
OOP - Carine Lucas 14 - 7
Parametric Overloading
4
14 - Polymorphism
Overriding
Example of Overriding
< aMagnitude
^self subclassResponsibility
= aMagnitude
^self subclassResponsibility
<= aMagnitude
^(self > aMagnitude) not
> aMagnitude
^aMagnitude < self
max: aMagnitude
self > aMagnitude
ifTrue: [^self]
ifFalse: [^aMagnitude]
OOP - Carine Lucas 14 - 10
5
14 - Polymorphism
Abstract methods
» Other names: deferred method, pure virtual
» An abstract method is a generalisation of
overriding
» Parent class names method, but provides no
implementation
» The child class must provide an
implementation
» Usually combined with other techniques
OOP - Carine Lucas 14 - 11
6
14 - Polymorphism
7
14 - Polymorphism
Templates
List<int> aList;
List<double> bList;
Polymorphism in C++
» Polymorphic variables only through pointers and references
» Without the virtual keyword, the dynamic type of the variable
(even a pointer or reference variable) is ignored when the
variable is used as a receiver for the associated message.
» Overriding only takes place with functions with exactly the
same number and types of arguments, otherwise parametric
overloading.
» Abstract functions need to be declared virtual and assigned
the value 0. Instantiation of a class that contains a pure virtual
function is not allowed.
» Generics are implemented through the keyword template.
OOP - Carine Lucas 14 - 16
8
14 - Polymorphism
Resolution of Overloaded
Functions in C++
Polymorphism in Java
» Variables can be declared through either a class or an
interface
» All variables are polymorphic:
» a variable declared by a class can hold values from any subclass
» a variable declared by an interface can hold values from any class
that implements the interface
» Abstract methods are declared through the keyword
abstract. A class that includes an abstract method must be
declared abstract. Instantiation of abstract classes is not
allowed.
» A class or method declared final can not be subclassed or
overridden.
OOP - Carine Lucas 14 - 18
9
14 - Polymorphism
Polymorphism in Smalltalk
10