Dynamic Binding is a way of binding the appropriate versions of derived classes which are inherited from same base class with Base class object.
Consider the following example:
Now, to call functions of all the 3 classes, we have to make object of each class, as follows:
C1 o1 =new C1();
C2 o2=new C2();
C3 o3=new C3();
Here, the “new” keyword has 2 functionalities:
- It helps to refer/saves/initializes the address of its RHS class to its LHS variable.
- It allocates sufficient memory space for the data members of the class.
To minimize the memory space & interface efficiency, we use the concept of dynamic binding as follows:
- o1= new C1();
- o1=new C2();
- o1=new C3();
- In this example, we can see in line #1, #3 and #5, the object o1 is an object of all the three classes C1, C2 and C3 respectively. So, this type of object is known as Polymorphic Object.
- In Line #2, #4 and #6, the statement “o1.add()” seems similar, but in every line the statement is executing from different scopes and having different results. Such types of statements are known as Polymorphic statements.
- In Line #2, #4 and #6, add () is polymorphic method.