Polymorphism in Object Oriented Programming (OOP)

In Programming Languages (C++, JAVA) Polymorphism (“Poly” (from Greek) means many and “morphe” means forms or shapes) is ability to provide a single interface to different entities which have different types.

Polymorphism is a technique in which different operations can have the same name, but they do slightly different work.

If you are not familiar with OOP Programming Please read following article first.

For example, consider an application that may draw three different types of graphs like line graph, pie graph and bar graph. We can draw algorithm of following problem as:

CASE FOR Graph type
IF Graph Type = Line Graph THEN Draw-line-graph (Data)
IF Graph Type = Pie Graph THEN Draw-Pie-graph (Data)
IF Graph Type = Bar Graph THEN Draw-Bar-graph (data)
End Case

Although this programming technique is very simple and straightforward, but adding new graph type could be difficult. The existing code would have to be updated accordingly.

In polymorphism, solution to problem exists. Suppose we define a base class “graph” and then define subclass for different types of graphs. Each subclass has a member function called “draw”. This concept called “Function Overriding”.

When an object is instantiated by a subclass receives a message regarding “draw” operation, it will invoke its own “draw” operation. When a new graph type is added in the system, a subclass with its own “draw” member function is developed. The remaining subclasses need not to be changed.

Polymorphism is the ability for objects of different classes related by inheritance to respond differently to the same function call. This technique helps to perform a number if different operations to have the same name.

Polymorphism is implemented through “Virtual Functions”. The overridden member function is called for execution through base class pointer. The program selects the correct overridden function in the appropriate derived class associated with the object.

By using the polymorphism feature, a program control might walk through a container, such as an array of pointers to object from various levels of a class hierarchy.

For example, an array of pointers to object of the base class “graph” may contain addresses of objects of derived classes line_graph, pie_graph,bar_graph etc. calling “draw” function will draw a correct graph using polymorphism.

POINTERS TO OBJECTS

A pointer to an object of a class can also be declared. The members of an object can be assessed through the pointer to an object. The arrow member selection operator (->) is used to access them, it is denoted by hyphen ( – ) and a greater than sign ( > ). This operator is also known as member access operators.

 The general syntax to access a member of an object through its pointer is as follows:

ptr -> member;

Where

  • ptr:    represents the name of pointer to an object.
  • ->:     represents the member access operator. It is used to access the member of the object through the pointer.
  • member: represents the name if the member to be accessed.

Example-1

A program example is given below in which pointer object of a class is declared and member function is accessed through member access pointer.

#include "iostream"
using namespace std;
class Polymorphism
{
   public:
   void test()
  {
    cout<<"Pointer To Object Called";
  }
};
main()
{
 Polymorphism p, *ptr;
 ptr = &p;
 p.test();
 ptr -> test();
}

Example-2

Write a program that has a base class and two derived classes. The base class and its
Derived classes have their member functions and the names of their member functions are same.


#include<iostream>
using namespace std;

class B
{
	public:
		void ppp()
		{
			cout<<"ppp() of the base class\n";
		}
}; 
 class D1 : public B
 {
 	public:
 		void ppp(void)
 		{
 			cout<<"ppp() of 1st derived class\n";
 		}
 }; 
   class D2 : public B
   {
   	public:
   		void ppp()
   		{
   			cout<<"ppp() of 2nd derived class\n";
   		}
   };
     main()
     {
     	B *p;
     	D1 x;
     	D2 y;
     	p = &x;
     	p->ppp();
     	p = &y;
     	p->ppp();
     }

Leave a Reply