Types Of Polymorphism

Introduction

Polymorphism means one name in many forms. It is the main feature of OOPs. Polymorphism is the ability to take more than one form, but the name will be the same. There are two types of polymorphism. NET.

Compile Time Polymorphism

When we create two or more methods with the same name but different parameters or different sequence of parameters and the time of calling compiler decide on the time of compilation which method should be called on the basis of given arguments.

That is why it is called compile-time polymorphism. We also call it static polymorphism.

It is implemented using overloaded methods and operators. The overloading process is called early binding. In compile-time polymorphism, I have implemented overloading concepts with an example given below.

Example 1. See the following example. In that example, I have created two Add methods whose names are the same but whose parameters are different.

using System;
namespace DemoTest
{
    class AddClass
    {
        public void Add(int a, int b)
        {
            int r = a + b;
            Console.WriteLine("Add two integer Numbers = " + r);
        }

        public void Add(string x, string y)
        {
            Console.WriteLine("Concatenation two strings = " + x + y);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // add two integer values
            AddClass addClassObj = new AddClass();
            Console.WriteLine("Enter Two Integer values");
            int m = int.Parse(Console.ReadLine());
            int n = int.Parse(Console.ReadLine());
            addClassObj.Add(m, n);
            // add two string values
            Console.WriteLine("Enter Two String values");
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();
            addClassObj.Add(s1, s2);
            Console.ReadLine();
        }
    }
}

Run Time Polymorphism

When we create the same name method in the inherited class and override the functionality of the base class, then we use Run Time Polymorphism. It is called run time polymorphism because the compiler decides which method should be called on the runtime. This is achieved through the use of virtual keywords with the method.

To override the base class method, we create the base class method as virtual and the derived class method as override.

Example 2. See the following example, where I have explained how we can override the base class method.

using System;
namespace DemoTest
{
    class ClassA
    {
        public virtual void Show()
        {
            Console.WriteLine("This is Show from ClassA");
        }
    }
    class ClassB : ClassA
    {
        public override void Show()
        {
            Console.WriteLine("This is Show from ClassB");
        }
    }
    class ClassC : ClassA
    {
        public override void Show()
        {
            Console.WriteLine("This is Show from ClassC");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ClassA classAObj = new ClassA();
            classAObj.Show(); // call classA "Show" method
            classAObj = new ClassB();
            classAObj.Show(); // call ClassB "Show" method
            classAObj = new ClassC();
            classAObj.Show(); // call ClassC "Show" method
            Console.ReadLine();
        }
    }
}

So, finally, you learned what polymorphism is and how many types of polymorphism. You also see the two examples which are explaining the compile time polymorphism and run time polymorphism.


Similar Articles