Forms of Polymorphism
Forms of Polymorphism
Forms of Polymorphism
Polymorphism refers to having more than one form. OOP helps to achieve polymorphism in
various ways.
1 Overloading:
Also called Ad-hoc Polymorphism.
Here, one name is used that refers to two or more different implementations.
A single function name denoting two or more function bodies is Function Overloading.
An operator used to perform the operation similar to basic data types in user defined data
types is Operator Overloading.
Both of these are forms of overloading.
2 Overriding:
Also called Inclusion Polymorphism.
A child class redefines a method inherited from a parent class.
3 Polymorphic variables:
Polymorphism employing polymorphic variables is also called Assignment Polymorphism.
It is a variable that can hold different types of values during the course of execution.
When a polymorphic variable is used as a parameter (argument) to a function, the
phenomenon is called “Pure polymorphism” or Polymorphic Method.
Example: a pointer to the base class can hold address of base class objects as well as the
address of all other types of derived class objects.
5 Generics:
It is the form of polymorphism achieved with the use of templates.
Templates help in creating general type of classes and functions by defining the parameters
used.
Class Template helps in creating various classes with single class definition.
Function Template helps in creating various functions with single function definition.
Prepared by Suman Ojha
Department of Computer Science and Engineering
Nepal Engineering College
Note:
A virtual function in a base class is also a virtual in derived class (if it gets inherited).
Method Invocation (Function call) is when you give a call to method.
Message Passing is passing objects as parameters (to a function).
Data contained in objects is called State of a Class and algorithm implemented in various
member functions is called Behavior of a Class.
References:
[1] http://www.cs.olemiss.edu/~hcc/csci581oo/notes/budd12.html