What is Polymorphism in Programming?

To deal with arguments of different types, Polymorphism was introduced. Read this article till the end to know more on polymorphism and its nature and variants.

Polymorphism is a technique to represent different types using a single type. In polymorphism, a common interface is developed to cater for the needs of requested events. 

In programming, programs are written to work on the values and manipulate them to provide results. Variables hold values in the program. To bind a variable to its corresponding value, machine-level instructions are executed. 

In computer programming, a variable is a memory location having a unique address. The value at that memory location is the value of the variable. Any value in computer science is represented by the pattern of bits. The value can be different as data can be of different types. Data is categorised into different types such as integer, float, string etc. A variable value can be accessed using the memory address of the variable. The name of the variable is independent of its value. Thus the name of the variable must be such that it should reflect the type of value it represents.

The variables in computer programming are bound to their respective value at the compile time. The bounded value is changed as the program execution proceeds. A variable can store the value of a particular type, such as an integer or string. 

A variable can be automatic or extern. An automatic variable comes into existence as the function execution begins and gets extinguished when function execution terminates. External variables are also global variables and can be accessed by any other function. Extern variable values persist throughout the program.

Values to variables are assigned using the assignment operator. A variable has an r-value that dictates its content and an l-value that dictates the location of the variable. l-value remains unchanged even after the execution of the expression. An r-value changes after the execution of the expression. The r-value of the variable is always a temporary value. 

The r-value and l-value correspond to parameters modes that are used in the prototype of the function. The r-value/l-value is related to input parameters and output parameters.

An input parameter accepts a value. The input parameters accept a value to maintain the data flow throughout the program. The output parameter returns value from the function. The output parameter is also known as return parameters or out parameters.

In a high-level programming language, l-values refer to the memory locations used to execute the program. Thus l-values are assigned values. In a high-level programming language, to retrieve the address of a variable & operator is used.

Polymorphism

A function contains a set of programming instructions that, when executed, can produce a value. The set of programming instructions can also be called an expression or a command. The function can take arguments, operate on these arguments and may or may not return a value.

Functions operate on different data types, such as integers and floating types. The data type the range of value and how this value can be stored in the memory. The data type determines the type of value that is being operated on. The type also determines the operators that can be used to manipulate the data type. In Computer Programming Languages, it is also possible that the same type of operator may act differently depending on the type of operands. When this happens, it is said that it is a polymorphic behaviour.

The data type determines the l-value and r-value. l-values are not considered when a value of a variable is assigned to another variable.

The polymorphism is the property to exhibit the behaviour of an object in more than one form. The concept of Polymorphism is seen in Object-Oriented Programming. In Object-Oriented Programming, the same function is represented in a different form. The polymorphism keeps the interface the same but depending on the nature of the function call, the behaviour of the function differs.

In Object-Oriented Programming, inheritance is the concept of reusing the class in a new class. Using inheritance subclassing is done. In subclassing, the properties of the main class or parent class are used in the subclass. The subclass is also called the derived class. The derived class can also add new functionalities. The objects of the derived class will have the functionalities of the parent class and the derived class.

Inheritance provides an abstract view of the class. Inheritance is used to establish the relationship between classes using different Object-Oriented Programming Concepts such as classification, specialisation, generalisation, approximation, and evolution. Thus inheritance is used for classification. 

Each class contains functions. Each function is capable of accepting arguments. In addition, a function can accept a permitted set of parameters from its domain. Each function is designed to compute values and provide results. The set of obtained results builds the function’s range. 

Thus functions are capable of returning a value. Furthermore, this value can be used to compute other values. Each function has an interface, and depending on the logic of the program number and type of parameter is decided.

Inheritance in Object-Oriented programming is done to achieve reusability. Reusability increases the productibility of the program. The objective of the concept of reusability is to develop special-purpose software than general-purpose software.

Polymorphism in Object-Oriented Programming gives rise to the concept of method overloading, operator overloading and virtual functions. The method overloading and operator overloading are compile-time polymorphisms, and the virtual function is run-time polymorphism.

In Object-Oriented programming, the class contains data member and member functions. The member functions are called by objects of the class. The call to the member function can be resolved at compile-time and run-time. Since Object-Oriented programming supports polymorphism, the call to the polymorphic behaviour can be done at compile-time or run-time. When a call to the polymorphic function is resolved at compile-time, then it is called compile-time polymorphism. When the call to the polymorphic function is resolved at run-time, then it is called run-time polymorphism.

In compile-time polymorphism, the function call is resolved based on the function prototype. The compile-time polymorphism is also known as early binding. To achieve compile-time polymorphism, method-overloading is done. 

In compile-time polymorphism, Inheritance is not used. An example of compile-time polymorphism is method overloading. 

In run-time polymorphism, the required function to be called is decided at run-time. Run time polymorphism is also known as dynamic binding.

Program example of Compile-time polymorphism is given below.

#include <iostream>
using namespace std;
class my_Addition 
{
    public:
          int my_sum(int my_num1,int my_num2) 
          {
            return my_num1+my_num2;
          }
          int my_sum(int my_num1,int my_num2, int my_num3) 
          {
            return my_num1+my_num2+my_num3;
          }
};

int main(void) 
{
    my_Addition my_obj;
    cout<<my_obj.my_sum(200, 150)<<endl;
    cout<<my_obj.my_sum(810, 1000, 100);
    return 0;
}
Output

350
1910
Code Analysis

The above code is an example of compile-time polymorphism. In this code, the class my_Addition has two functions with the same name my_sum(int,int) and my_sum(int,int,int). 

When the call to the function is made, the function call is resolved at compile-time based on the number of parameters.    
#include <iostream>
using namespace std;

class my_Base 
{
   public:
    void my_print() 
    {
        cout << "This is Base Class Function" << endl;
    }
};

class my_Derived : public my_Base 
{
   public:
    void my_print() 
    {
        cout << "This is Derived Class Function" << endl;
    }
};

int main() 
{
    my_Derived my_derived1;
    my_derived1.my_print();
    return 0;
}
Output

This is Derived Class Function
Code Analysis

The above code is an example of Run-time polymorphism. In this example, the base class and the derived class function with the name my_print( ). 

The function to be called resolved at run-time. 

When the object of the derived class makes a call to the function my_print( ), the call is resolved at run-time.