Introduction to Object Oriented Programming, 3rd Ed

Timothy A. Budd

Chapter 14

Polymorphism and Software Reuse

Outline

  1. Roadmap
  2. Definition of Polymorphic
  3. Major Forms of Polymorphism in Object Oriented Languages
  4. Two Approaches to Software Reuse
  5. Example -- Building Sets from Lists
    1. Using Inheritance
    2. Using Composition
  6. Advantages and Disadvantages of Each Mechanism

Other Material

Intro OOP, Chapter 14, Outline

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

Intro OOP, Chapter 14, Slide 08