Polymorphism

Polymorphism

Table of Content:

  1. what is polymorphism?
  2. Types of Polymorphism with Example

What is Polymorphism?

  The term Polymorphism is derived from the Greek word where poly + morphos.where poly means many and morphos means forms.
In other words Polymorphism means many forms.
Polymorphism is another Important feature of OOP(Object Oriented Programming). In programming Polymorphism allows same Object to behave differently base on given condition.Polymorphism is another concept of object-oriented programming (OOPs). The attitude which lies beneath this concept is "single interface having multiple implementations." This provides a single interface for controlling access to a general class of actions.

Occurrence of polymorphism:

  polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ Polymorphism:

  C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Real Life Example of Polymorphism

 miss.Ashley is an teacher to her students and employee for Principle/School administrator.and daughter to her Mother.
 here miss.Ashley plays different roles based on place/situation.

In Programming Polymorphism is divide into Two parts:

  1. Static polymorphism (also known as compile time polymorphism)
  2. Dynamic polymorphism (also known as Run time Polymorphism)

Static polymorphism:

Static polymorphism or early binding this type of binding or polymorphism is achieved by Function Overloading or Operator Overloading.
For Example we have two functions with same name but different number of arguments. Based on how many parameters we pass during function call determines which function is to be called, this is why it is considered as an example of polymorphism because in different conditions the output is different. Since, the call is determined during compile time thats why it is called compile time polymorphism or early binding.

 Function Overloading:

When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.

Example: 

 OutPut:

 Operator overloading:

we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add to operands. So a single operator ‘+’ when placed between integer operands , adds them and when placed between string operands, concatenates them.

Example 

OUTPUT:

Dynamic polymorphism:

Dynamic polymorphism or Run time polymorphism is achieved by method overriding/virtual method.

What is Virtual Function?

A virtual function can be defined as the member function within a base class which you expect to redefine in derived classes. For creating a virtual function, you have to precede your function's declaration within the base class with a virtual keyword.

Example :

OUTPUT:

Thank You

Your question are welcome!

Comments