How To Achive Polymorphism In C#

Introduction

According to MSDN:

    Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means "many-shaped".

In simple terms, in Polymorphism an object can take make forms.

Note: To understand polymorphism, make sure you have understood the concepts of Inheritance. As we know that the Polymorphism can’t achieved without Inheritance.

Description:

Polymorphism can be achived in two ways, That is:

  1. Static Polymorphism or Compile time Polymorphism

    Compile time polymorphism is nothing but the method overloading in C#. In simple terms we can say that a class can have more than one method with same name but with different number of arguments or different types of arguments or both.

    Compile time polymorphism can be archived in Single Class.

    Constructor can be overloaded.

    Example
    1. namespace Polymorphism  
    2. {  
    3.     class MyClass  
    4.     {  
    5.         public void show()  
    6.         {  
    7.             MessageBox.Show("This is 'Show' Without Argumant ");  
    8.         }  
    9.         public void show(int num1, string str1)  
    10.         {  
    11.             MessageBox.Show("This is 'Show' With 2 Argumant ");  
    12.         }  
    13.         public void show(string str1, int num1)  
    14.         {  
    15.             MessageBox.Show("This is 'Show' With 2 Argumant Difference in Sequence of Paramanters");  
    16.         }  
    17.         public void show(string str1, string str2)  
    18.         {  
    19.             MessageBox.Show("This is 'Show' With 2 Argumant but Different in Type ");  
    20.         }  
    21.         public void show(string str1, string str2, string str3)  
    22.         {  
    23.             MessageBox.Show("This is 'Show' With 3 Argumant Difference in Number of Paramanters");  
    24.         }  
    25.     }  
    26.     class AnotherClass  
    27.     {  
    28.         public void Display()  
    29.         {  
    30.             //we have Called all the Oveloaded Methods by using single instance of the class.  
    31.             MyClass obj = new MyClass();  
    32.             obj.show();  
    33.             obj.show(1, "test");  
    34.             obj.show("text", 5);  
    35.             obj.show("AAA""BBB""CCC");  
    36.             obj.show("AAA""XXX");  
    37.         }  
    38.     }  
    39. }  
  2. Dynamic Polymorphism or Runtime Polymorphism

    Child class has the same method as of base class.

    Dynamic Polymorphism can be archived using method overriding. Overriding means a derived class is implementing a method of its super class.

    Example
    1. namespace Polymorphism  
    2. {  
    3.     class ParentClass  
    4.     {  
    5.         //Add virtual keyword to make sure that this methord will be override in child class.  
    6.         public virtual void show()  
    7.         {  
    8.             MessageBox.Show("");  
    9.         }  
    10.         public virtual void Display()  
    11.             {  
    12.                 MessageBox.Show("");  
    13.             }  
    14.             //this is not a virtual method , and it can't be override  
    15.         public void Disp()  
    16.         {  
    17.             MessageBox.Show("");  
    18.         }  
    19.     }  
    20.     class ChildClass: ParentClass  
    21.     {  
    22.         //we have override the method in Child class which is marked as virtual.  
    23.         public override void show()  
    24.             {  
    25.                 MessageBox.Show("");  
    26.             }  
    27.             //we have override the method in Child class which is marked as virtual.  
    28.         public override void Display()  
    29.         {  
    30.             MessageBox.Show("");  
    31.         }  
    32.     }  
    33. }  

Important points

Polymorphism provides the functionality to Call Child class methods using instance of Parent class that is not there in Inheritance.

The “new” keyword is used to hide the method of parent or base class which we don’t own. This is called method hiding.


Similar Articles