Polymorphism Introduction - Object Orientated Programming

Basic Polymorphism Note: this tutorial assumes you are already familiar with classes, objects and inheritance.

Polymorphism is the name given to one of the fundemental principles of object orientated programming (OOP). In fact, if a programming language does not support polymorphism it cannot call itself OOP. All major OOP languages support polymorphism, these include: C++, Java, C# and Python. At the moment polymorphism is probably quite intimidating to you - this is the fear of the unknown and is completely natural. Just as with any unknown subject, once some light has been shed on it you'll realise how simple it really is and wonder why you never learned it sooner.

Polymorphism: the ability of different objects to respond, each in its own way, to the same message.

So, what does this mean? well...it means we can treat any objects of a derived class in a generalized way, and send them all the same message -- supporting the principle of OOP.

Let's say we have a 'Base class' called Vehicle...this class will define all the attributes and behaviours common and shared among all vehicles. An obvious attribute would be the current 'speed' of the vehicle and an obvious behaviour could be 'accelerate()'. In our example we will have 4 types of vehicle:

Bicycle, Car, Airplane and Floating Log

"Floating Log??", well it can act the same way as a vehicle...as a means of travel you could sit on it and float down a river. Now, if I wanted all my 4 vehicles to accelerate I could just simple call their member functions like so:

bicycle.accelerate();
car.accelerate();
airplane.accelerate();
floating_log.accelerate();

This however, assumes the calling function knows what type of vehicle/object it is dealing with beforehand. This is both sloppy coding and defeats the entire purpose of the OOP paradigm.

Another thing you could do is have the same accelerate() member function inherited from the Vehicle base class for every derived class. This would fix the prior problem of the calling function needing to know the vehicle derived type, but it is still unsuitable. imagine calling:

bicycle.accelerate();
airplane.accelerate();

only to have a bicycle accelerate at the same velocity as a jet!

As you can see. Objects created from the same base class can have very similar behaviour, but respond very differently and in their own unique way. This is where polymorphism steps in and helps us out.

some polymorphism code:

const int NUM = 4;

Vehicle *vehicles[NUM] = (
    new bicycle(),
    new car(),
    new airplane(),
    new floating_log());

for (int i = 0; i < NUM; i++) {
    vehicle[i]->accelerate();
}

I have intentionally left out language specific class definitions to avoid confusion. As you can see, every time we call accelerate() on a particular object it will override the base class member function of accelerate() and put it's own member function in it's place. Depending on the objects type, different member functions will be called.

Conclusion:
Polymorphism allows you to send the same message to different objects that are derived from the same base class, these objects can then respond how they choose. This is a very powerful feature and is one of the pillars of OOP programming.

jump to top