Polymorphism

Inheritance in Object Oriented Programming (OOP) lends itself to another powerful feature known as Polymorphism.  I will speak about this in context of of the programming language C++.  In an over simplistic explanation polymorphism can be seen as function and/or operator overloading.  This is because polymorphism uses inheritance and virtual functions to achieve a very cool effect.

Perhaps the easiest way to convey this idea is through the use of an example. Take for example an Object Animal, which is the super class for Tiger and Elephant.  In the object Animal we can have a method called Move.  However, when we call Move on an Tiger we expect that the Tiger will move faster then the Elephant.  This means the method is specialized in the derived class.  This is a core feature of Inheritance, and nothing new.  The method Move would most likely be implemented as pure virtual method making the Animal object and Abstract Data Type.

The magic comes when we want to call Move on a pointer to the base object Animal regardless of the type of Animal.  I try to illustrate this point with this small block of code.

Animal* pAnimal = NULL;
// Create an instance of Tiger and call Move on it
pAnimal = new Tiger();
pAnimal -> Move();

// Clean up
delete pAnimal; pAnimal = NULL;

// Create an instance of Elephant reusing the same pointer and call Move on it
pAnimal = new Elephant();
pAnimal->Move();

The ability to call Move with a pointer of the base kind encapsulates the general idea of Polymorphism.  Using Polymorphism allows you to write re-usable methods or modules of code that do not depend on the derived object.  For example we could create a vector of animals and pass them to a function which would call move on all Animals passed.

void moveAllAnimals( stl::vector& animals ){
     for ( int index = 0; animals->size(); ++index )
          animals[index]->Move();
}

Down-casting

If you have a pointer to an object of type animal and you want to at run time gain access to a specific Type of Animal you can down cast the pointer.  In the case where you do not know the kind of Animal that is being pointed to, a check can be implemented to verify that you do indeed have an Animal of the type that you wanted. In C++ the dynamic cast function is used to down cast a pointer.

Tiger* pTiger = dynamic_cast< pAnimal >;
if ( pTiger ) {
     // Call specific code on the Tiger Animal
}

Operator Overloading

In the beginning of this post I mentioned that Polymorphism is used with operator overloading.  The most common example of this and easiest to understand is the +operator.  The plus operator can be used on various types int, string, floats and the correct operation must be performed based on the types being added.

Advertisements
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s