Chapter 14, Outline
Introduction to Object Oriented Programming, 3rd Ed
Chapter 14
Polymorphism and Software Reuse
Outline
- Roadmap
- Definition of Polymorphic
- Major Forms of Polymorphism in Object Oriented Languages
- Two Approaches to Software Reuse
- Example -- Building Sets from Lists
- Using Inheritance
- Using Composition
- Advantages and Disadvantages of Each Mechanism
Other Material
Roadmap
In this chapter and the four that follow we will start to investigate
polymorphism, as it is found in many forms in object-oriented languages.
In this chapter we will outline the four major types of polymorphism,
then explore them in more detail in the later chapters.
The different mechanisms each have the same goal of encouraging
software reuse, facilitating ease of understanding and speeding
application development.
Intro OOP, Chapter 14, Slide 01
Definition of Polymorphic
Polymorphous: Having, or assuming, various forms, characters, or styles.
From greek routes, poly = many, and Morphos = form (Morphus was the greek
god of sleep, who could assume many forms, and from which we derive the
name Morphine, among other things).
A polymorphic compound can crystalize in many forms, such as carbon, which can
be graphite, diamonds, or fullerenes.
In programming languages, used for a variety of different mechanisms.
Usage of the term is confused by the fact that it means something
slightly different in the functional programming community than it
does in the OO world.)
Intro OOP, Chapter 14, Slide 02
Major Forms of Polymorphism in Object Oriented Languages
There are four major forms of polymorphism in object-oriented languages:
- Overloading (ad hoc polymorphism) -- one name that refers to
two or more different implementations.
- Overriding (inclusion polymorphism) -- A child class
redefining a method inherited from a
parent class.
- The Polymorphic Variable (assignment polymorphism) -- A
variable that can hold different
types of values during the course of execution. It is called Pure Polymorphism
when a polymorphic variable is used as a parameter.
- Generics (or Templates) -- A way of creating general tools or classes
by parameterizing on types.
Intro OOP, Chapter 14, Slide 03
Two Approaches to Software Reuse
One of the major goals of OOP is software reuse.
We can illustrate this by considering two different approaches to
reuse:
- Inheritance -- the is-a relationship.
- Composition -- the has-a relationship.
We do this by considering an example problem that could use either
mechanism.
Intro OOP, Chapter 14, Slide 04
Example - Building Sets from Lists
Suppose we have already a List data type with the following behavior:
Want to build the Set data type (elements are unique).
class List {
public:
void add (int);
int includes (int);
void remove (int);
int firstElement ();
};
Intro OOP, Chapter 14, Slide 05
Using Inheritance
Only need specify what is new - the addition method.
Everything else is given for free.
class Set : public List {
public:
void add(int);
};
void Set::add(int x)
{
if (! includes(x)) // only include if not already there
List::add(x);
}
Intro OOP, Chapter 14, Slide 06
Using Composition
Everything must be redefined, but implementation can make use of the
list data structure.
class Set {
public:
void add(int);
int includes(int);
void remove(int);
int firstElement();
private:
List data;
};
|
void Set::add (int x)
{
if (! data.includes(x))
data.add(x);
}
int Set::includes (int x)
{ return data.includes(x); }
void Set::remove (int x)
{ data.remove(x); }
int Set::firstElement ()
{ return data.firstElement(); }
|
Intro OOP, Chapter 14, Slide 07
Advantages and Disadvantages of Each Mechanism
-
Composition is simpler, and clearly indicates what operations
are provided.
-
Inheritance makes for shorter code, possibly increased functionality,
but makes it more difficult to understand what behavior is being
provided.
-
Inheritance may open to the door for unintended usage, by means
of unintended inheritance of behavior.
-
Easier to change underlying details using composition (i.e., change
the data representation).
-
Inheritance may permit polymorphism
-
Understandability is a toss-up, each has different complexity issues
(size versus inheritance tree depth)
-
Very small execution time advantage for inheritance
Intro OOP, Chapter 14, Slide 08