Oct 23, 2015

What is Polymorphism



   Before going to that, I will explain a real world example.

If you give an instruction "cut" to three different people – a hair stylist, a doctor and a movie actor. How do they respond?  The hair stylist cuts the hair.  A doctor will cut the patient’s body for surgery, a movie actor will stop acting. The message (cut) is same to all three objects (actor, hair stylist and a doctor), but they three respond in three different ways. This is called as Polymorphism.

Polymorphism: Poly – Multiple and Morph – Form

Polymorphism refers to the ability of two or more objects belonging to different classes to respond differently to the same message in class specific ways.

It means the ability to take more than one form. An operation may exhibit different behaviour in different instances. The behaviour depends on the types of data used in the operation. For example, consider Addition operation. For two numbers, the operation will generation sum. In case of two strings, it will generate a third string with concatenation. Thus, polymorphism allows objects with different internal structure to share same external interface, though specific actions associated with each operation (function) may differ.

How is polymorphism implemented ?
·       The fundamental idea of polymorphism is that a compiler doesn’t know which method (i.e. base class implementation or derived class implementation of a method) to call during  the compile time.  It only knows during run time (based on the object). This is called as “Late Binding”.  This type of polymorphism is called as “Run time Polymorphism” or "Polymorphism by Inheritance"
·       Polymorphism can also be achieved through method overloading.  During the compile time itself the compiler knows which method to execute. This is known as “Early Binding”. This type of polymorphism is called as “Compile time Polymorphism” 

There are two types of polymorphism:

1. Compile time Polymorphism

2. Run time Polymorphism

Compile time Polymorphism:

One function - multiple purpose
or
 In short, Many functions having same name but with different function body. For every function call, compiler binds the call to one function definition at compile time. This decision of binding among several functions is taken by considering formal arguments of the function, their data type and their sequence.

Example :

void f(int i){cout<<"int";}
void f(char c){cout<<"char";}
int main()
{
   f(10);
   return 0;
}
Output: int

Run time polymorphism:

C++ allows binding to be delayed till run time. When you have a function with same name, equal number of arguments, same data type in same sequence in base class as well derived class and a function call of form:
base_class_type_ptr->member_function(args);
will always call base class member function. The keyword virtual on a member function in base class indicates to the compiler to delay the binding till run time.
Every class with atleast one virtual function has a vtable (virtual function table)  that helps in binding at run time. Looking at the content of base class type pointer, it will correctly call the member function of one of possible derived / base class member function.
Let’s say that we have a base class called Class A and it has a over-ridable method called print which prints all the attributes.  Then by inheritance concept, all the derived class will inherit this print method. The derived classes can either override the print method or use the print method from base class.  So, all the objects of base class and derived class have a method called print and they respond differently to a message based on its class implementation.

Example:

class Base
{
public:

    virtual void display(int i)
    { cout<<"Base::"<<i; }
};

class Derv: public Base
{
public:

    void display(int j)
    { cout<<"Derv::"<<j; }
};

int main()
{
    Base *ptr=new Derv;
    ptr->display(10);
    return 0;
}
Output:  Derv::10

As the base class has virtual function, the statement

Base *ptr=new Derv;
will point to the derived class and execute the display function that has been defined in the derived class. If virtual keyword is not declared, then the base class display function will be called.




Filed under  |