Polymorphism

Polymorphism :

  • The word polymorphism means having many forms.
  • In inheritance, polymorphism is done, by method overriding, when both super and sub class have member function with same declaration but different definition, and method overloading.
  • When there are more than one function with same name but different signatures then, its called Function Overloading
  • If we inherit a class into the derived class and provide a definition for one of the base class’s function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding.
Lets consider a following program :

OUTPUT :

In the above program, when display() of class B is called the display() method of B is invoked not of A, though, it inherits class A.This is called static binding i.e. the function call is fixed before the program is executed. This is also sometimes called early binding because the display() function is set during the compilation of the program.

Now, lets consider another example :

OUTPUT :

Here, we tried to call the display() by using parent B. We stored the reference of A’s object in B and then called display(). Here, we get display() of B only because compiler on seeing Parent class’s pointer, set call to parent class’s display() function, without knowing the actual object type.

When compiler determines the type of object at runtime, and then binds the function call, then it is called Late Binding. It is also called Dynamic Binding or Runtime Binding.
In simple words, in Late Binding function call is resolved at runtime.

To achieve it Virtual Functions are used.

VIRTUAL FUNCTIONS

ABSTRACT CLASSES

Leave a Reply

Your email address will not be published. Required fields are marked *