C# Polymorphism with Examples

In c#, Polymorphism means providing an ability to take more than one form and it’s a one of the main pillar concept of object oriented programming, after encapsulation and inheritance.

 

Generally, the polymorphism is a combination of two words, one is poly and another one is morphs. Here poly means “multiple” and morphs means “forms” so polymorphism means many forms.

 

In c#, polymorphism provides an ability for the classes to implement a different methods that are called through the same name and it also provides an ability to invoke the methods of derived class through base class reference during runtime based on our requirements.

 

In c#, we have a two different kind of polymorphisms available, those are

 

  • Compile Time Polymorphism
  • Run Time Polymorphism

C# Compile Time Polymorphism

In c#, Compile Time Polymorphism means defining a multiple methods with same name but with different parameters. By using compile time polymorphism, we can perform a different tasks with same method name by passing different parameters.

 

In c#, the compile time polymorphism can be achieved by using method overloading and it is also called as early binding or static binding.

 

Following is the code snippet of implementing a method overloading to achieve compile time polymorphism in c#.

 

public class Calculate

{

    public void AddNumbers(int a, int b)

    {

        Console.WriteLine("a + b = {0}", a + b);

    }

    public void AddNumbers(int a, int b, int c)

    {

        Console.WriteLine("a + b + c = {0}", a + b + c);

    }

}

If you observe above “Calculate” class, we defined a two methods with same name (AddNumbers) but with different input parameters to achieve method overloading, this is called a compile time polymorphism in c#.

C# Compile Time Polymorphism Example

Following is the example of implementing a compile time polymorphism in c# programming language.

 

using System;

 

namespace Tutlane

{

    public class Calculate

    {

        public void AddNumbers(int a, int b)

        {

            Console.WriteLine("a + b = {0}", a + b);

        }

        public void AddNumbers(int a, int b, int c)

        {

            Console.WriteLine("a + b + c = {0}", a + b + c);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Calculate c = new Calculate();

            c.AddNumbers(1, 2);

            c.AddNumbers(1, 2, 3);

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

When we execute above c# program, we will get the result like as shown below.

 

C# Compile Time Polymorphism Example Result

 

This is how we can achieve compile time polymorphism in c# by implementing a method overloading based on our requirements.

C# Run Time Polymorphism

In c#, Run Time Polymorphism means overriding a base class method in derived class by creating a similar function and this can be achieved by using override & virtual keywords along with inheritance principle.

 

By using run time polymorphism, we can override a base class method in derived class by creating a method with same name and parameters to perform a different task.

 

In c#, the run time polymorphism can be achieved by using method overriding and it is also called as late binding or dynamic binding.

 

Following is the code snippet of implementing a method overriding to achieve run time polymorphism in c#.

 

// Base Class

public class Users

{

    public virtual void GetInfo()

    {

        Console.WriteLine("Base Class");

    }

}

// Derived Class

public class Details : Users

{

    public override void GetInfo()

    {

        Console.WriteLine("Derived Class");

    }

}

If you observe above code snippet, we created a two classes (“Users”, “Details”) and the derived class (Details) is inheriting the properties from base class (Users) and we are overriding the base class method GetInfo in derived class by creating a same function to achieve method overriding, this is called a run time polymorphism in c#.

 

Here, we defined a GetInfo method with virtual keyword in base class to allow derived class to override that method using override keyword.

 

Generally, only the methods with virtual keyword in base class are allowed to override in derived class using override keyword. To know more about it, check this Method Overriding in C#.

C# Run Time Polymorphism Example

Following is the example of implementing a run time polymorphism in c# programming language.

 

using System;

 

namespace Tutlane

{

    // Base Class

    public class BClass

    {

        public virtual void GetInfo()

        {

            Console.WriteLine("Learn C# Tutorial");

        }

    }

    // Derived Class

    public class DClass : BClass

    {

        public override void GetInfo()

        {

            Console.WriteLine("Welcome to Tutlane");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            DClass d = new DClass();

            d.GetInfo();

            BClass b = new BClass();

            b.GetInfo();

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

When we execute above c# program, we will get the result like as shown below.

 

C# Runtime Polymorphism Example Result

 

This is how we can achieve run time polymorphism in c# by implementing a method overriding based on our requirements.  

 

PREVIOUS

C# Base Keyword
 
 

Topics Covered