Friday, September 7, 2007

Polymorphism

Polymorphism means that the same thing can exist in two forms. This is an important characteristic of true object oriented design - which means that one could develop good OO design with data abstraction and inheritance, but the real power of object oriented design seems to surface when polymorphism is used.

In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts.
For example, the + (plus) operator in C++:
4 + 5 <-- integer addition 3.14 + 2.0 <-- floating point addition s1 + "bar" <-- string concatenation!
In C++, that type of polymorphism is called overloading.


Polymorphism

  • Literal meaning - many forms
  • OOP meaning
    - Same method (message) but different behavior
    - Object determines which method gets executed
  • Function or Method Overloading
    - Similar message but different behavior
  • Abstract interfaces to class hierarchies
    - Define core data and behavior capabilities of a class family
    - All classes in the family support core capabilities
    - All objects in family respond to core methods (messages)

A classic example is how area is calculated on different shapes. We define an abstact base class shape and derive two classes - rectangle and circle from it. An area() method defined in the base class will have to be implemented differently in rectangle and circle, since it has to be calculated differently.

This is an example of polymorphic behavior, and in C++, this is implemented using virtual member functions. The area() member function is defined virtual in the base class, which signals to the compiler that this member function has to be invoked from the correct derived class at run time. This is indeed a run time decision, and which derived class member function is to be invoked depends on which derived class pointer has been assigned to the base class pointer. This also brings in the concept of late binding (run time binding), since the above association to the area member function can only be done at run time. The compiler will have to insert extra code to do the late binding, which adds in a little runtime overhead.


Types of Polymorphism:
C++ provides three different types of polymorphism.
Virtual functions
Function name overloading
Operator overloading

In addition to the above three types of polymorphism, there exist other kinds of polymorphism:
run-time
compile-time
ad-hoc polymorphism
parametric polymorphism


Other types of polymorphism defined:
run-time:
The run-time polymorphism is implemented with inheritance and virtual functions.
compile-time:
The compile-time polymorphism is implemented with templates.
ad-hoc polymorphism:
If the range of actual types that can be used is finite and the combinations must be individually specified prior to use, this is called ad-hoc polymorphism.
parametric polymorphism:
If all code is written without mention of any specific type and thus can be used transparently with any number of new types it is called parametric polymorphism.

In general, there are two main categories of Polymorphism namely
Ad Hoc Polymorphism
Pure Polymorphism


Overloading concepts fall under the category of Ad Hoc Polymorphism and Virtual methods. Templates or parametric classes fall under the category of Pure Polymorphism.

No comments: