next up previous contents
Next: A Word of OOP Up: An Object-Oriented World Previous: Inheritance Example   Contents

Polymorphism Example

The Greek word polymorphism means one name, many forms and is, maybe, the most difficult term to explain, especially in real-world terminology. Let us look at a car again and say that polymorphism is somewhat akin to the automatic transmission in your car. Suppose, it has three functions: Neutral, Drive, Reverse.

We know for sure, that there is only one method refers to as Reverse. Whenever we select Neutral, the engine of a car is disconnected from the transmission mechanism.

While we are driving a car to move forward, we select Drive. Depending on various set of conditions at runtime, the automatic transmission system mechanism decides which version of the Drive function to apply in every concrete situation. The specific version of the function will be used depending on the current situation. This is an analogue to so-called runtime polymorphism.

Therefore, the car exhibits polymorphic behaviour when going in the forward direction, but exhibits non-polymorphic behaviour when going backwards.

Polymorphism is used to customise the behaviour of an instance of a type (an object) based on existing conditions.

We can summarise our examples:

We live in the object-oriented world. The object-oriented programming paradigm attempts to express computer programs in such ways that model how we understand the world.


next up previous contents
Next: A Word of OOP Up: An Object-Oriented World Previous: Inheritance Example   Contents
Olexiy Ye. Tykhomyrov 2001-12-16