Topic 3: Polymorphism and Overloading
Synopsis
Polymorphism¤
-
Ever been in a singles bar?
-
A standard stimulus (message) provokes a unique response
-
Depending on the class¤
of the object you stimulate
-
This kind of behaviour¤
is called polymorphism.
Polymorphism¤
and Inheritance
-
The reason we can send a particular message to an object is because it
belongs to a class¤
for which that operation (receiving that message) is well-defined
-
Once a message is well-defined for a given class¤
it is automatically well-defined for any subclasses¤
-
However, we may wish to change (override) the way in which a specific
subclass¤
responds to a given message
-
If we are accessing an object via a pointer to its superclass¤,
that pointer could legitimately be pointing to an object of any
subclass¤
Polymorphism¤
and Abstraction¤
-
Can view polymorphism¤
as a form of abstraction¤
-
Abstraction¤
of function
Polymorphism¤
and Typing¤
-
Polymorphism¤
is an example of type enforcement: if an object is of type T it must respond
to a given message as a T would
-
Polymorphism¤
is also an example of dynamic typing¤¤
(late binding¤),
because the actual type enforcement isn't done until the message is actually
sent (i.e. you don't know how the object will respond until you send the
message)
Polymorphic (or Dynamic)
Dispatch¤
-
Normally in an OO program, when we specify that a message is to be sent
to an object the response is determined by the compiler (by looking at
the types of the objects and messages involved)
-
The compiler then translates that request into the equivalent assembler-level
subroutine call
-
This is called static dispatch
-
However, with polymorphism¤,
the code to dispatch¤
the message to the right handler (i.e. assembler subroutine) can't
be determined by the compiler
-
This is because the necessary information (namely the actual type of the
object) isn't available at compile time
-
Hence the compiler must instead insert code that will determine and invoke
the appropriate assembler subroutine at the point when the message is actually
sent
-
This is called dynamic dispatch
Overloading
-
Polymorphism¤
can also apply to functions and operations
-
For example: what does a/b do?
Overloaded operations
-
An operation is overloaded if its behaviour¤
depends on the type of one or more of its arguments
-
Note that this is (usually) statically determined
-
Some languages (including C++) allow the programmer to define additional
overloadings for various operators
- Why?
Overloaded functions¤
-
Another way to look at overloaded operators¤
is to treat (say) integer division and floating-point division as entirely
separate operations which just happen to have the same name
- Likewise we can envisage two or more functions with different behaviour¤
but the same name
-
For example: sizeof(), abs(), print(),
etc.
Dispatching overloaded
functions¤
-
How can the compiler tell which overloaded function¤
to call?
-
How does it tell which overloaded operator¤
to call?
-
According to the types of the arguments provided
-
Hence functions and operators may have the same name but must have a unique
signature
Reading
- Lippman & Lajoie
- Revision: Chapters 4 and 5.
- New: Sections 9.1 and 15.1.
- Stroustrup
- Revision: Chapter 6.
- New: Sections 7.4 to 7.7.
- Sommerville:
- Chapter 13
This material is part of the CSE2305 - Object-Oriented
Software Engineering course.
Copyright © Jon McCormack & Damian Conway, 1998-2005. All rights reserved.