What is Polymorphism in OOPS?

In the real world, it often happens that one person is doing multiple tasks. For example, you work as an employee in a company and at another time, you play the role of a son in your home. However, you don’t change your name every time. You just put different attires for different roles. This concept is not restricted to the Real World, it is also applied to Programming. In this article, we will learn what is Polymorphism in OOPs.

Polymorphism, which means ‘One name and many forms’ is one of the seven principles of Object Oriented Programming. It simply means that one function can be used for multiple operations, without declaring a different function. Since the function's name is the same for all use cases, it’s become crucial to know how to identify the operation of the function in different cases.

As you already know the standard paradigm of Programming includes two stages- Compilation and Execution. So, the function’s behavior may be known during the compilation stage or execution of the code.

This differentiates the type of Polymorphism implemented in your code. Before learning what is Polymorphism in OOPS, just go through the diagram below to understand its classification. Let’s discuss the first type of Polymorphism.

Compile Time Polymorphism

Compile Time Polymorphism means function definitions are distinguished at the time of compilation. It is applied using the concept of Overloading and is further classified as Function Overloading and Operator Overloading.

Function Overloading

In Function Overloading, you can define more than one function with the same name but different arguments. These different arguments help the compiler to identify which function definition should be called.

Let’s understand this with a C++ implementation.

#include<iostream>
#include<string.h>
using namespace std;
class Person{
    public:
    void display(string name){
        cout<<"Name : "<<name<<endl;
    }
    void display(int age){
        cout<<"Age : "<<age<<endl;

    }
    void display(double salary){
        cout<<"Salary: "<<salary<<endl;
    }

};
int main(){
    Person object;
    object.display("John");
    object.display(22);
    object.display(45500.500);
}

Here the display function has different arguments in all the cases using which compiler knows which function definition to call. The code as following output:

Name : John
Age : 22
Salary: 45500.5

write your code here: Coding Playground

An important point here is you have to use different arguments in the function definitions. If you want to use the same arguments, then you should use the different order of passing arguments as shown below.

class Person{
    public:
    void display(string name, int age){
        cout<<"Name : "<<name<< ", " << "Age : "<<age<<endl;
    }
    void display(int age, string name){
        cout<<"Age : " << ", " << " Name : "<<name<<endl;
    }
   

};

Like Function Overloading, Operator Overloading is also an important concept while learning what is Polymorphism in OOPS as Object Oriented Programming allows us to also use operators for this purpose.

Operator Overloading

Operator Overloading allows us to redefine how operators work under normal conditions. Before learning what is Polymorphism in OOPS, you should be aware of the basic operators used in C++. There are three types of Operators:

  • Unary Operator
  • Binary Operator
  • Ternary Operator

However, in Operator Overloading, the following operators cannot be overloaded:

  • Scope Operator (::)
  • Member Access Operator (.)
  • Asterisk (*)
  • Ternary Operator (?:)

Now, we will look into the Unary and Binary Operator Overloading under Polymorphism.

For Unary Operator Overloading, suppose you want to redefine the (++) operator and increase the value by 100, instead of 1. Then, you have to define the (++) operator as shown below and then call the operator using the object of the class.

#include<iostream>
using namespace std;
class Programming{
    public:
    int val;
    //unary operator overloading
    void operator ++(){
        val+=100;   
    }
};
int main(){
    Programming obj;
    obj.val=10;
  ++obj;//calling the overloaded operator
    cout<<obj.val;
}

As shown in the below output, the value is incremented by 100.

110

Similarly, Binary Operator Overloading is done. Normally, the Addition (+) operator is used to add two built-in Data Types. But, using Operator Overloading, can be used to add two numbers of different data types. Below is the C++ implementation of Binary Operator Overloading.

#include<iostream>
using namespace std;
//Binary Operator Overloading for (+) operator
class Programming{
    private:
    int feet;
    int inches;
    public:
    //constructor to initialize default values as 0
    Programming(){
        feet=0;
        inches=0;
    }
    //for assigning values
    void assign(int f, int in){
        this->feet=f;
        this->inches=in;
    }
    //defining overloaded operator
    Programming operator +(const Programming & object){
        //result is added to another object answer which is returned
        Programming answer;
        answer.feet= feet+object.feet;
        answer.inches= inches+object.inches;
        return answer;
    }
    void display(Programming ob){
        cout<<"Sum is: ";
        cout<<ob.feet<<" feet " << ob.inches<<" inches "<<endl;
    }
};

int main(){
    Programming obj1,obj2,obj3;
    //initializing the numbers
    obj1.assign(2,3);
    obj2.assign(4,5);
    //obj2 will be passed as an argument when obj1 calls the (+) operator
    obj3=obj1+obj2;
    obj3.display(obj3);
}

The output of the code is:

Sum is: 6 feet 8 inches 

Now, to completely understand what is polymorphism in OOPS, we have to understand the second type i.e. Run Time Polymorphism.

Run Time Polymorphism

In this type of Polymorphism, the function definitions are distinguished during the execution of the code. Run Time Polymorphism is achieved by implementing the virtual function. To understand the Virtual Function, you should be familiar with Inheritance in OOPS

A Virtual Function is a member function that is declared in the Parent class and then it is redefined in the Child Class.

At the time of calling the function, instead of an object, a pointer of the base class is used to call the function. Which function is called is decided by which object is referenced by the pointer. This is decided later during run time it is called Run Time Polymorphism. The following code shows Run Time Polymorphism using Virtual Functions.

#include<iostream>
using namespace std;
class Shape{
    public:
    virtual void display(){
        cout<<"This is General Shape"<<endl;
    }
};
class Circle:public Shape{
    public:
    void display(){
        cout<<"This is Circle"<<endl;
    }
};
int main(){
    Shape *pointer;
    Shape obj;
    Circle cir;
    pointer=&obj ;// giving reference to base class object
    pointer->display();
    pointer=&cir;//giving reference to the derived class object
    pointer->display();
}

The output of the code is shown below.

This is General Shape
This is Circle

write your code here: Coding Playground

Now, it would be clear to you what is polymorphism in OOPS. Moreover, virtual functions are mainly used to use the base class pointers which can be used to call the derived class methods without knowing the Derived class object.