OOPs

What is Polymorphism in OOPs

What is Polymorphism in OOPs

Polymorphism

Polymorphism is the ability to treat various objects in the same manner. "Poly" means many, and "morph" means forms.
In other words, one object has many forms or has one name with multiple functionalities. Polymorphism allows a class to have multiple implementations with the same name
Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance.


Types of Polymorphism
Static / Compile Time Polymorphism
Dynamic / Runtime Polymorphism



Compile-Time Polymorphism (Method Overloading Or early binding Or Static binding)
This type of polymorphism is achieved by function overloading or operator overloading.

A. Function Overloading
When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded,
Functions can be overloaded by changing the number of arguments or/and changing the type of arguments or/and order of parameters.
The return type of the methods does not play any role in the method overloading.

Invoking Overloaded Methods
We can call the overloaded method by passing the exact parameter it requires in exact same sequence.

//Different number of parameters of the same type.
class ConsolePrinter
{
    public void Print(string str){
        Console.WriteLine(str);
    }

    public void Print(string str1, string str2){
        Console.WriteLine($"{str1}, {str2}");
    }

    public void Print(string str1, string str2, string str3){
        Console.WriteLine($"{str1}, {str2}, {str3}");
    }
}

OR


//Using the same number of parameters with different types
class ConsolePrinter
{
    public void Print(string str){
        Console.WriteLine(str);
    }

    public void Print(int a){
        Console.WriteLine($"Integer {a}");
    }
}

//Same number of parameters with different sequences.
class ConsolePrinter
{
    public void Print(int a, string str){
        Console.WriteLine($"{a}, {str}");
    }

    public void Print(string str, int a){
        Console.WriteLine($"{a}, {str}");
    }
}


B. Operator Overloading
An Operator can give different outputs based on the type of operands that it is operating on. This is called Operator Overloading.
E.g., the operator ‘+’ can perform addition on two integers, while it can concatenate two strings. Thus the same operator can be used in 2 different ways.

class Complex
{
    public double x;
    public double y;
   
    // no-argument constructor
    public Complex() {}
    // parameterized constructor
    public Complex(double real, double img)
    {
        x = real;
        y = img;
    }

    // Overloading of Binary "+" operator
    public static Complex operator + (Complex c1, Complex c2)
    {
        Complex c3 = new Complex();
        c3.x = c1.x + c2.x;
        c3.y = c1.y + c2.y;
        return c3;
    }
   
    // function to display result
    public void display()
    {
        Console.WriteLine("{0} + {1}i", x,y);
    }
}


class DoCalculation {
    static void Main(string[] args)
    {
        Complex num1 = new Complex(2.5,3.5);
        Complex num2 = new Complex(1.2,6.5);
        Complex num3 = num1 + num2;

        Console.Write("c1 = ");
        num1.display();
        Console.Write("c2 = ");
        num2.display();
        Console.Write("c3 = ");
        num3.display();
    }
}



Runtime Polymorphism  (Late binding Or Dynamic polymorphism)
This type of polymorphism is achieved by Function / Method Overriding using inheritance.
The function call is resolved at runtime in runtime polymorphism.'

The process of re-implementing the superclass non-static, non-private, and non-sealed method in the subclass with the same signature is called Method Overriding in C#. The same signature means the name and the parameters (type, number, and order of the parameters) should be the same.
With method overriding, it is possible for the base class and derived class to have the same method name and the same signature.

class Class1
{
    //Virtual Function (Overridable Method)
    public virtual void Show()
    {
        //Parent Class Logic Same for All Child Classes
        Console.WriteLine("Parent Class Show Method");
    }
}
class Class2 : Class1
{
    //Overriding Method
    public override void Show()
    {
        //Child Class Reimplementing the Logic
        Console.WriteLine("Child Class Show Method");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Class1 obj1 = new Class2();
        obj1.Show();
        Class2 obj2 = new Class2();
        obj2.Show();
    }
}

// Show same output in both cases - Child Class Show Method


Why in both cases the Child Class Show Method is Invoked?
Here, Class1 is the superclass and Class2 is the subclass. The point that you need to keep in mind is that the Super Class Reference Variable Can hold the Subclass object reference and but the reverse is not possible i.e. Sub Class Reference Variable can never hold the Super Class Object Reference. So, the Class1 reference variable can hold the Class2 object reference.




Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram