Polymorphism In OOPS: What is Polymorphism [Detailed Explanation]

Polymorphism in OOPs is inseparable and an essential concept of every object-oriented programming language. An object or reference basically can take multiple forms in different instances. As the word suggests, ‘poly’ means ‘many’ and ‘morph’ points at ‘forms’; thus, polymorphism as a whole would mean ‘a property of having many forms.’

The object-oriented programming language processes classes and objects by a single interface. It implements the concepts of function overloading, overriding, and virtual functions. Also, it is typically used for instrumenting inheritance in programming. 

Read: Polymorphism vs Inheritance

An Example of Polymorphism in OOPs

Polymorphism is the method in an object-oriented programming language that performs different things as per the object’s class, which calls it. With Polymorphism, a message is sent to multiple class objects, and every object responds appropriately according to the properties of the class. 

Following is the code that explains polymorphism clearly:

use warnings;
# Creating package class
package A;
# Creating Constructor
sub new
{
# shift will take package name
# and assign it to variable ‘class’
my $class = shift;
my $self = {
‘name’ => shift,
‘roll_no’ => shift
};
sub method
{
print(“This belongs to class A “);
}
};
package B;
# The @ISA array contains a list
# of that class’s parent classes, if any
my @ISA = (A);

submethod
{
print(“This belongs to class B”);
}
package main;
B->method();
A->method();

Output:

This belongs to class B

This belongs to class A

Learn to build applications like Swiggy, Quora, IMDB and more

Explanation

For the first output, the method() defined in class B overrides the definition inherited from class A and vice-versa for the second output. This property is useful in extending any pre-existing package’s functionality without rewriting the entire class’s entire definition. Thus, making the job easy for the programmer and the reason for being used widely.

Types of Polymorphism in Oops

In Object-Oriented Programming (OOPS) language, there are two types of polymorphism as below:

  1. Static Binding (or Compile time) Polymorphism, e.g., Method Overloading
  2. Dynamic Binding (or Runtime) Polymorphism, e.g., Method overriding

Source

1. Compile Time or Static Polymorphism

With Method Overloading, static polymorphism is achieved in Object-Oriented Programming languages that allow the programmer to implement various methods. The names they use can be the same, but their parameters are different. Certain conditions are conducive for static polymorphism as below:

  •       Types of All Parameters should be different.
  •       The sequence of the Parameters can be different.
  •       The number of parameters of one method should differ from the other method.

In the static binding polymorphism, the matching type and number of arguments invoke the overloaded functions. 

  • As all of this information is available during the compile time, the compiler selects the appropriate function. 
  • The function overloading does it, and operator overloading is also termed as static binding or early binding.

Program

class A                                  //  base class

{

int a;

public:

void display()

{

cout<< “Class A “;

}

};

class B: public A                       //  derived class

{

int b;

public:

void display()

{

cout<<“Class B”;

}

};

In the above program, the display() function prototype is the same in both the base and derived classes. Therefore, static binding cannot be applied here. This program would run best at the run time with an appropriate selection of functions.

  • During the run time, the language’s compiler identifies various methods by identifying signatures of those methods.
  • The compiler first identifies the method signature and decides the method for a specific method call during program compilation.  
  • The execution for Compile time Polymorphism is much faster but the process is not so flexible. 

Also Read: OOPS Interview Questions

Runtime or Dynamic Polymorphism

In the Dynamic Polymorphism, a call to a single overridden method is solved during a program’s runtime. Method overriding is one of the prominent examples of Runtime Polymorphism. In this process, the overriding is done through pointers and virtual functions.

  • In Method Overriding, a single method is declared in a sub-class present in a parent class. The child class gains a method for implementation.
  • During Runtime Polymorphism, the class offers the specification of its own to another inherited method. This transfer between methods is achieved without modifying the parent class object codes.

Program

 #include <iostream>    

using namespace std;    

class Animal {                                          //  base class  

    public:    

    string color = “Brown”;      

};     

class Dog: public Animal                       // inheriting Animal class.  

{      

 public:    

    string color = “Green”;      

};    

int main(void) {    

     Animal d= Dog();      

    cout<<d.color;     

}    

Output:

Brown 

  • In the Run time polymorphism, the object’s method is invoked at the run time instead of compile time. 
  • It is made possible by method overriding, which is also coined as dynamic binding or late binding. 
  • The process of dynamic polymorphism is comparatively slower but flexible than compile-time polymorphism.

Differences B/W Compile-Time and Run-Time Polymorphism 

  Compile-Time Polymorphism Run-Time Polymorphism
Invoking of Function The function is invoked at the compile time. The function is invoked at the run time.
Common Terms It is known as overloading, early binding, and static binding. It is known as overriding, late binding, and dynamic binding.
Method Name and Parameters In Overloading, more than one method has the same name but with a different number or type of parameters. In Overriding, more than one method has the same name, number, and type of parameters.
Carriers It is achieved with function and operator overloading. It is achieved with virtual functions and pointers.
Execution Time It executes faster than run-time polymorphism at the compile time. It executes slower than compile-time polymorphism at the run time.
Flexibility It is less flexible as everything executes at the compile time. It is more flexible as everything executes at the run time.

Final Words

Polymorphism is the must learn concepts of OOPs as it offers several advantages while programming. With Polymorphism, the user can remodel the tested classes and codes. It is useful in extending the programming by itself. It also allows the user to store multiple different type variables such as double, Float, Int, or Long in a single variable to make it simpler to search and implement them. 

If you’re interested to learn more about OOPs, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

Leave a comment

Your email address will not be published.

×