What is Polymorphism ?
Polymorphism comes from the Greek words “poly” and “morphism”. “poly” means many and “morphism” means form i.e.. many forms. Polymorphism means the ability to take more than one form.
Polymorphism allows a single name to be reused for several related but different purposes. The purpose of polymorphism is to allow one name to be used for a general class. Depending on the type of data, a specific instance of the general case is executed.
Polymorphism is subdivided in two concepts Compile Time( static) polymorphism and Run Time (dynamic) polymorphism.
Types of Polymorphism
- Compile Time Polymorphism
- function overloading
- operator overloading
- Run Time Polymorphism
- virtual function
1. Compile Time Polymorphism
Compile Time Polymorphism can be achieved using two ways listed below.
Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. Overloading of functions with different return type is not allowed. View full Topic.
Operator overloading is the ability to tell the compiler how to perform a certain operation based on its corresponding operator‟s data type. Like + performs addition of two integer numbers, concatenation of two string variables and works totally different when used with objects of time class. View full Topic.
2. Run Time Polymorphism
Run Time Polymorphism is also known as the Dynamic binding or Dynamic polymorphism. It refers to linking of procedure call to the code to be executed in response to the call. Dynamic binding(or late binding) means the code associated with a given procedure call in not known until the time of call at run time.
Dynamic binding is the linking of a routine or object at runtime based on the conditions at that moment. It means that the code associated with a given procedure call is not known until the time of the call. At run-time, the code matching the object under current reference will be called.
In C++, virtual methods are used to implement polymorphic classes.
Virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. The whole function body can be replaced with a new set of implementation in the derived class. It is declared as virtual in the base class using the virtual keyword. View full Topic.