Polymorphism, Run Time Polymorphism and Compile Time Polymorphism in C#

 

In one my previous article we discuss about OOP’s concepts in C#. In this article I explain about Polymorphism one of OOP’s concept, run time polymorphism and compile time polymorphism.

 

Polymorphism is the ability of two different objects to respond to the same request message in their own unique way.We can achieve polymorphism through method overloading and method overriding. Method overloading is called Compile time polymorphism and method overriding is called Run time polymorphism.

 

Here I will explain Compile Time Polymorphism and Run Time Polymorphism with simple example. For example create sample Employee base class with CheckRole() method as shown below.

 

namespace PolymorphismExp

{

    class Employee

    {

        public string CheckEmpRole(int empId)

        {

            return "Employee";

        }

    }

}

 

Compile Time Polymorphism or Method Overloading:

Method overloading or compile time polymorphism means changing the method behaviour at compile only. That means if any class has two methods with same name but different in signature is called compile time polymorphism or method overloading.

 

Add one more CheckRole() method to Employee class which takes one more parameter role also as shown below.

 

namespace PolymorphismExp

{

    class Employee

    {

        public string CheckEmpRole(int empId)

        {

            return "Employee";

        }

 

        public string CheckEmpRole(int empId, string role)

        {

            return "Employee Role: " + role;

        }

    }

}

 

This is called compile time polymorphism because here we are changing the method behavior at compile time only.

 

Run Time Polymorphism or Method Overriding:

Method overriding or Run time polymorphism means changing the base class method behavior in child class. We can change the base class method behavior in child class by applying virtual keyword for base class method and override keyword for child class method

 

Create two classes Employee.cs(which is base class) and another one is Manager.cs(which is  Child Class) as shown below.

 

namespace PolymorphismExp

{

    class Employee

    {

        public virtual string CheckEmpRole(int empId)

        {

            return "Employee";

        }

    }

}

 

namespace PolymorphismExp

{

    class Manager:Employee

    {

        override string  CheckEmpRole(int empId)

        {

            return "Employee is Manager";

        }

    }

}

 

As shown above, base class Employee.cs has CheckEmpRole() method prefixed with virtual keyword and child class Manager.cs inheriting the base class Employee. In Manager class we are overriding the child class method CheckEmpRole() method by using override keyword. This is called run time polymorphism or method overriding because we are changing the method behavior in child class at run time through inheritance.

 

                                                                                                         PolymorphismExp.zip (21.33 kb)