- Polymorphism is one of the another most important features of object-oriented programming.
- It simply means “one name multiple forms‟.
- The word polymorphism consists of two greek words – ‘poly’ means many/several and ‘morphs’ means form/structure, signifies the many uses of these operators and function in different situations.
- Polymorphism is an operation that may exhibit different behaviour in different instances. The behaviour depends upon the types of data used in the operation.
- Polymorphism is a mechanism that allows to implement a function in different ways in different situations or environment.
- Polymorphism is the ability to use an operator or method/function in different ways.
- Polymorphism refers to codes, operations or objects that behave differently in different contexts.
- Polymorphism gives different meanings to the operators or methods.
- Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface i.e., a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ.
- Condition to occur polymorphism, the following conditions must apply –
- All different classes must be derived from a single base class.
- The member function must be declared virtual in the base class.
Suppose an expression
2 + 3 //This represents integer addition.
Again the same + operator can be used with different meanings with strings such as :
“Coders” + “helpline” //This represents String addition.
Once again, the same + operator can be also used for floating point addition such as :
2.321+5.021 //This represents floating point addition.
Thus, we saw that from above given example that a single operator “+‟ behaves differently in different contexts such as integer, string or float referring the concept of polymorphism. The above polymorphism concept is said to be an operator overloading type. When the existing operator or function operates on new data type more than once, it is said to be overloaded.
Types of Polymorphism
- C++ shows two type of polymorphism –
(i) Static/Compile-time/Early Binding or linking polymorphism and
(ii) Run-time/Dynamic Binding polymorphism.
- Function overloading & operator overloading are the example of compile time polymorphism whereas run-time polymorphism can be achieved by the use of both derived classes and virtual functions.
- In polymorphism, a single function or an operator functioning in many ways depends upon the usage to function properly.
Static/Compile-time/Early Binding Polymorphism
- When the same operator (in operator overloading) and function (in function overloading)works differently on different arguments producing different results.
- In function overloading, the overloaded member functions are selected for
invoking by matching arguments, in both datatype and number of arguments. This information is known to the compiler in advance/at the compile time and, therefore, compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding/static binding/static linking/compile time polymorphism.
Run-time/Dynamic Binding polymorphism.
- When the selection of the appropriate function in a program occurs dynamically at run time, it is called as dynamic binding.
- C++ supports run-time polymorphism by a mechanism called virtual function. It exhibits late binding.
Benefits/Advantages of Polymorphism
- The main advantage of polymorphism is creation of reusable code by the programmer’s – classes once written, tested and implemented can be easily reused at another required places without caring about what’s written in the case.
- By using polymorphic variables memory is defined once, i.e. a single variable can be used to store multiple data types (integers, strings, etc.) rather than declaring a different variable again and again for each data format to be used.
- A polymorphic program can be easily extended i.e. once an application is written using the concept of polymorphism, it can easily be extended, providing new objects that conform to the original interface. Also, It is unnecessary to recompile original programs again when new type is added. Only re-linking is necessary to exhibit the new changes along with the old application. It is one of the greatest feature of oops.
- It provides easier maintenance of applications.
- It helps in achieving robustness in applications.
- In inheritance, If a derived class defines same function as defined in its base class, it is known as function overriding in C++.
- It is used to achieve runtime polymorphism i.e. Overriding is a type of polymorphism.
- It enables us to provide specific implementation of the function which is already provided by its base class.
- It is sometimes also called Method overriding which is the ability of the inherited class rewriting the virtual method of the base class.
- Overriding is about same method, same signature but different classes connected through inheritance.
- Constructors are not normal methods and they cannot be overridden.
- The word “virtual‟ means something that does not exist in reality, some sort of imaginary thing.
- The concept of virtual function is the same as a member function, but it does not really exist although it appears in needed places in a program.
- The object-oriented programming language C++ implements the concept of virtual function as a simple member function, like all member functions of the class.
- The functionality of virtual functions can be overridden in its derived classes.
- Polymorphism is used to give different meanings to the same concept. This is the basis for virtual function implementation.
- When the form/nature of a member function of a class is changed at runtime. Such member functions are called virtual functions and the corresponding class is called polymorphic class. The objects of the polymorphic class, addressed by pointers, change at runtime and respond differently for the same message.
Pure Virtual Functions
- It is also called do-nothing functions/dummy functions.
- This pure virtual functions is always virtual function.
- Pure virtual functions are functions with null body defined in base class.
- This makes a derived class able to override them.
- Any normal function cannot be declared as pure function.
- A pure virtual function cannot be used for any operation.
- It cannot be used to declare object.
Pure Abstract/Abstract Class
- When a class contains a pure virtual function it is called Abstract Class.
- It cannot be used to declare any object i.e. any attempt to declare an object for an abstract class result in an error.
- To declare pure virtual functions –
virtual void display()=0;
- Here, the assignment operator is not used to assign zero to this function rather used to instruct the compiler that the function is pure.
- It cannot be used to declare object.
Demerits/Disadvantages of Polymorphism
- Implementation of polymorphism is difficult, for the programmer in programming codes.
- Run time polymorphism leads performance issue because os with compilers manages all the related resources suddenly at run time.
- Polymorphism reduces the readability of the program i.e. to identify the runtime behavior of a program is difficult one.
635 total views, 1 views today