Basically polymorphism represents poly means many and morph means forms that many forms.
In which we are passing same message to different objets but every object will work for that message in their own manner. Means message is same but implementaion is different.
Take an real life example:
Suppose I have set of 50 LED TV's. There are two cases.
Case 1: Each tv has seperate remote but it is very difficult for me to manage each tve remote.
Case 2: I can use common remote to manage all LED Tv's. So it is very esay for me to manage all tv's with the help of common remote.
So second case represents polymorphism concept. We are providing common message but its implementaion is different.
Before going to polymorphism we will understand what is mean by binding.Binding represents association of object with function. There are two types of binding:
1. Compile time binding (Early binding or static binding): At compile time, binding takes place that will be representd as compilr time binding.
2. Run time binding (Late binding or dynamic binding): At run time, binding takes place that will be represented as run time biding.
So we have to understood the concept of run time binding before that we need to understand two concepts.
1. Function overloading: Same function name but different signature represents function overloading. It will always takes place into same classes. Signature means may be no of parameters,sequence and its return type may be different.
2. Function overriding: All things of function(Function name,signature, return type) are same then it will be represented as function overriding. But function overriding always takes place in to different classes.
For example:
class A
{
public:
void show()
{
cout<<"In class A"<<endl;
}
};
Class B : public A
{
void show()
{
cout<<"In class B";
}
}
So In above example void show() function represents Function overrding concept. Function overriding always takes place into different classes when we are using inheritance concept.
Now let's see polymorphism concept:
For example:
class A
{
public:
void show()
{
cout<<"In class A"<<endl;
}
};
class B: public A
{
void show()
{
cout<<"In class B";
}
}
void main()
{
A *p; //base class pointer
B bObj; //object of derived class
p = &bObj; //assigned address of class B object to base class pointer p.
p->show(); //Trying to call derived class function withe the help of base class pointer.
}
In above example I am trying to call function of dereived class B. But here compile time binding takes place. Here compiler calls base class function because the compilre will check type of pointer. So we have declared pointer is type of Class A. Here compilre is not going to consider what address stored within pointer. It will just consider what type of pointer we have created and it will call base class function.
So how to resolve this problem. So for that we need to apply run time polymeophsim. In C++ run time polymeorphism is achieved by using virtual keyword. Here we have to just make base class function as virtual. No need to use virtual keyword for each overrided function. Need to make base class function as virtual. Here all ovrrided function automatically made virtual because we using inheritance concept.
If we are using function overriding then and then your derived classes functins become virtual.
So just consider following change in above same example.We are just making void show() function of base class A as a virtual.
For example:
class A
{
public:
virtual void show()
{
cout<<"In class A"<<endl;
}
};
class B : public A
{
void show()
{
cout<<"In class B";
}
}
void main()
{
A *p; //base class pointer
B bObj; //object of derived class
p = &bObj; //assigned address of class B object to base class pointer p.
p->show(); //It will call appropriate function of class B.
}
So now we will get expected output because we have applied run time polymorphism. We have just made base class function as a virtual. Now compiler going to check at run time what type of address we have stored within this base class pointer. So it will call appropriate function of derived class.
So in this way we have achieved polymorphism with the help of virtual keyword. So we are calling here derived classes function with the help of base class pointer.