C# Polymorphism with Examples

C# Polymorphism with Examples

Polymorphism is a Greek word,the meaning of Polymorphism is “one name many forms”. In other words, one object has many forms or has one name with multiple functionalities. “Poly” means many and “morph” means forms.

Polymorphism provides the ability to a class to have multiple implementations with the same name. It is one of the core principles of Object Oriented Programming after inheritance and encapsulation. In this article, you’ll learn what polymorphism is, how it works, and how to implement polymorphism in C#.

In c#, there are two different kind of polymorphisms available, those are

  • Compile Time Polymorphism
  • Run Time Polymorphism

Compile Time Polymorphism :

Compile Time Polymorphism means defining a multiple methods with same name but with different parameters or signature.

The compile time polymorphism can be achieved by using method overloading and it is also called as static binding or early binding.

C# Compile Time Polymorphism Example

using System;
namespace OppsConcept
{
  class Program
  {
    static void Main(string[] args)
    {
      Calculate c = new Calculate();
      c.Multiplication(5,10);
      c.Multiplication(5,10,15);
      Console.WriteLine("Enter Key to Exit..");
      Console.ReadLine();
    }

  public class Calculate
  {
     public void Multiplication(int a, int b)
     {
       Console.WriteLine("a * b = {0}", a * b);
     }

    public void Multiplication(int a, int b, int c)
    {
      Console.WriteLine("a * b * c = {0}", a * b * c);
    }
   }
 }
}

When we execute above C# program, we will get the result like below.

Compile Time Polymorphism

Run Time Polymorphism :

Run Time Polymorphism means overriding a base class method in derived class by creating a similar method 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 signature/parameters to perform a different task.

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

In c#, run time polymorphism can be achieve by using following code snipets.

using System;
namespace OppsConcept
{
 class Program
 {
   static void Main(string[] args)
   {
     BClass b = new BClass();
     b.GetDetail();
     AClass a = new AClass();
     a.GetDetail();
     Console.WriteLine("Enter Key to Exit !!!!!");
     Console.ReadLine();
    }

   // Base Class

   public class AClass
   {
     public virtual void GetDetail()
     {
       Console.WriteLine("Learn polimorphism");
      }
    }

// Derived Class

  public class BClass : AClass
  {
     public override void GetDetail()
     {
        Console.WriteLine("Welcome to C#");
     }
   }
  }
}

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

Also observe in code, for base class (AClass) we have used virtual key word before void and in child class(BClass) we have used override key word before void to achieve overriding of base class.

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

 

Run Time Polymorphism

Facebook Notice for EU! You need to login to view and post FB Comments!

5 Comments

Leave a Reply

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