Types Of Polymorphism

Polymorphism means one name many forms. It is the main feature of OOPs. Polymorphism is an ability to take more than one form but name will be the same. There are two types of polymorphism in NET.

Compile Time Polymorphism

When we create two or more method with the same name but different parameters or different sequence of parameters and the time of calling compiler decide on the time of compilation which method should be called on the basis of given arguments.

That is why it is called compile time polymorphism. We also call it static polymorphism.

It is implemented using overloaded methods and operators. Overloading process is called early binding. In compile time polymorphism I have implemented overloading concepts with an example as given below:

Example 1

See the following example and in that example I have created two Add methods whose name are same but parameters are different.

  1. using System;  
  2.   
  3. namespace DemoTest  
  4.  {  
  5.      class AddClass  
  6.      {  
  7.          public void Add(int a, int b)  
  8.          {  
  9.              int r = a + b;  
  10.              Console.WriteLine("Add two integer Number = " + r);  
  11.          }  
  12.          public void Add(string x, string y)  
  13.          {  
  14.              Console.WriteLine("Concatenation two string = " + x+y);  
  15.          }  
  16.      }  
  17.   
  18.      class Program  
  19.      {  
  20.          static void Main(string[] args)  
  21.          {  
  22.              //add two integer values  
  23.              AddClass addClassObj = new AddClass();  
  24.              Console.WriteLine("Enter Two Integer values");  
  25.              int m = int.Parse(Console.ReadLine());  
  26.              int n = int.Parse(Console.ReadLine());  
  27.              addClassObj.Add(m, n);   
  28.   
  29.              //add two string values  
  30.              Console.WriteLine("Enter Two String values");  
  31.              string s1 = Console.ReadLine();  
  32.              string s2 = Console.ReadLine();  
  33.              addClassObj.Add(s1, s2);   
  34.              Console.ReadLine();  
  35.          }  
  36.      }  
  37.  }  
Run Time Polymorphism

When we create same name method in inherited class and what to override the functionality of base class, then we use “Run Time Polymorphism”. It is called run time polymorphism because the compiler decides which method should be called on the runtime. This is achieved through the use of virtual keyword with method.

To override the base class method, we create base class method as virtual and derived class method as override.

Example 2

See the following example where I have explained how can we override the base class method.
  1. using System;  
  2.   
  3. namespace DemoTest  
  4. {  
  5.     class ClassA  
  6.     {  
  7.         public virtual void Show()  
  8.         {  
  9.             Console.WriteLine("This is Show from ClassA");  
  10.         }  
  11.     }  
  12.     class ClassB : ClassA  
  13.     {  
  14.         public override void Show()  
  15.         {  
  16.             Console.WriteLine("This is Show from ClassB");  
  17.         }  
  18.     }  
  19.     class ClassC : ClassA  
  20.     {  
  21.         public override void Show()  
  22.         {  
  23.             Console.WriteLine("This is Show from ClassC");  
  24.         }  
  25.     }  
  26.     class Program  
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             ClassA classAObj = new ClassA();  
  31.             classAObj.Show(); // call classA "Show" method  
  32.             classAObj = new ClassB();    
  33.             classAObj.Show(); // call ClassB "Show" method  
  34.             classAObj = new ClassC();   
  35.             classAObj.Show();   // call ClassC "Show" method  
  36.             Console.ReadLine();  
  37.         }  
  38.     }  
  39. }  
So, finally you learned what polymorphism is and how many types of polymorphism. You also see the two examples which are explaining the compile time polymorphism and run time polymorphism.

 

Up Next
    Ebook Download
    View all
    Learn
    View all