Polymorphism and Types

Polymorphism means one name in many forms. This is the main feature of OOP. Polymorphism is the ability to take on multiple forms, but the name remains the same. There are two types of polymorphism.

  1. Compile Time Polymorphism
  2. Run Time Polymorphism

If you create two or more methods with the same name but different parameters or different parameter sequences and invocation times, the compiler determines which method to call based on the arguments specified at compile time. This is why it’s called compile-time polymorphism. Also called static polymorphism. Implementation is done using overloaded methods and operators. The overloading process is called early binding. Regarding compile-time polymorphism, I have implemented the overloading concept using the 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 TestClass
    {
        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 ****
            TestClassaddClassObj = new TestClass();
            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

Use runtime polymorphism when you want to create a method with the same name in an inherited class to override functionality in the base class. This is called runtime polymorphism because the compiler decides which method to call at runtime. This is achieved by using the virtual keyword within the method. To override a method in a base class, create the method in the base class as virtual and create the method in the derived class as an override.

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

using System;
namespace DemoApp
{
    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(); // It will call classA "Show" method
            
            classAObj = new ClassB();
            classAObj.Show(); // It will call ClassB "Show" method
            
            classAObj = new ClassC();
            classAObj.Show(); // It will call ClassC "Show" method
            Console.ReadLine();
        }
    }
}

I hope you enjoyed reading this post. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such posts.

Leave a Reply

Your email address will not be published. Required fields are marked *