You are on page 1of 10

14 - Polymorphism

Polymorphism

» Purpose of this lecture


» to discuss different ways to achieve
polymorphism in the different languages

OOP - Carine Lucas 14 - 1

Definition

» Polymorphous: having or assuming various


forms, characters or styles (Webster’s
Collegiate Dictionary, fifth edition)
» Is used in various different ways in
computer science

OOP - Carine Lucas 14 - 2

1
14 - Polymorphism

Polymorphism in programming
languages

» A variable that can hold, at different times,


values of many different types
» A name associated with several different
function bodies
» A single function that has at least one
argument which is a polymorphic variable

OOP - Carine Lucas 14 - 3

Polymorphism in untyped
languages

» Polymorphism deals with types, and is thus


a trivial issue in untyped languages
» All variables are potentially polymorphic
silly: x
“a silly Smalltalk polymorphic method”
(x isKindOf: Integer) ifTrue: [^x+1].
(x isKindOf: Fraction) ifTrue: [^x reciprocal].
(x isKindOf: String) ifTrue: [^x reversed].
^nil

OOP - Carine Lucas 14 - 4

2
14 - Polymorphism

Forms of polymorphism

» polymorphic variables
» pure polymorphism (functions with
polymorphic arguments)
» overloading
» overriding
» abstract or deferred methods

OOP - Carine Lucas 14 - 5

Polymorphic variables

» A variable declared as an instance of one


class can hold a value from a subclass type
» Dynamic (run-time) type must be subclass of
static (declared) type
» In C++ only works with pointers and references
» The polymorphic variable (or value) is basis for
much of the power of OOP
» Example: static CardPile
allPiles[];
OOP - Carine Lucas 14 - 6

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

» Selection of which procedure to execute is made based on


number and types of arguments
» type signature determines the procedure to execute
» C++ and Java allow functions in the same scope to have
the same name as long as type signature is different
stream << integer;
stream << char;
stream << char*;
stream << double;
stream << complex;

OOP - Carine Lucas 14 - 8

4
14 - Polymorphism

Overriding

» Overriding occurs when a child class changes the


meaning of a function defined in a parent class
» Different child classes can override in different
ways
» Parent class can have default behaviour, child
classes alternative
» Contributes to code sharing
» Ensures common interfaces

OOP - Carine Lucas 14 - 9

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

Motivation for Abstract Methods

» Allows to reason on a higher level of


abstraction.
» Provides a common interface for static
typing, as validity of message passing is
decided on static type.
» Example: common interface on CardPile
allows sending messages display and
addCard to all elements of allPiles.
OOP - Carine Lucas 14 - 12

6
14 - Polymorphism

Deferred methods in C++


class Shape {
public:
Point corner;
void virtual draw() = 0;
};

class Circle: public Shape {


public:
int radius;
void draw() { drawCircle(corner + radius, radius); }
};

OOP - Carine Lucas 14 - 13

Generics and templates


» Yet another form of polymorphism is the use of
generics or templates.
» A type can be used as a parameter in a class
description
template <class T> class List {
public:
void add(T);
T firstElement();
T value;
list<T>* nextElement;
};
OOP - Carine Lucas 14 - 14

7
14 - Polymorphism

Templates

List<int> aList;
List<double> bList;

template <class T> int length(List<T> & aList)


{ if (aList == 0)
return 0;
return
1 + length(aList.nextElement)

OOP - Carine Lucas 14 - 15

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++

» When searching for a function to match to a


particular call, the compiler
» seeks the most enclosing name scope in which
the function name has been defined
» then seeks to match the function in that scope
on the basis of argument type

OOP - Carine Lucas 14 - 17

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

» Because Smalltalk is dynamically typed,


all variables are polymorphic; they can
hold any value.
» No explicit declaration for abstract classes
and methods. Use of message
subclassResponsibility.

OOP - Carine Lucas 14 - 19

Efficiency and polymorphism

» Programming always involves compromises


» Polymorphism is a compromise between
efficiency and ease of development, use and
maintenance
» Who’s time is more important, yours or the
computer’s?

OOP - Carine Lucas 14 - 20

10

You might also like