Polymorphism in C#

Polymorphism
 
Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means “multiple” and morph means “forms” so polymorphism means many forms.
 
Polymorphism can be either static or dynamic. Based on the binding the method polymorphism can be differentiate into two types,
  1. Static polymorphism
  2. Dynamic Polymorphism 
Static Polymorphism :

The mechanism of linking a method with an object during compile time is called early binding. In static polymorphism the compiler will decide which method to be called.
 
Function overloading and operator overloading are the examples for static binding (or static polymorphism). 
 
Function overloading : 

The process of creating more than one method in a class with the same name is called function overloading.

While overloading methods, we need to follow the overloaded methods which must differ either in number of arguments they take or the data type of at least one argument.

Note : A method with the same name and same signature but with different return type can't be overloaded. 
  1. class SumClass  
  2. {  
  3.     public int Sum(int A, int B)  
  4.     {  
  5.         return A + B;  
  6.     }  
  7.   
  8.     public float Sum(float A, float B)  
  9.     {  
  10.         return A + B;  
  11.     }  
  12.   
  13.     public int Sum(int A, int B, int C)  
  14.     {  
  15.         return A + B + C;  
  16.   
  17.     }  
  18. }  
  19.   
  20.   
  21. class MainClass  
  22. {  
  23.     static void Main()  
  24.     {  
  25.   
  26.         SumClass sumClass = new SumClass();  
  27.   
  28.         Console.WriteLine("sum of 2 integers (10, 15) : " + sumClass.Sum(10, 15));  
  29.   
  30.         Console.WriteLine("sum of 2 floats (2.3f, 5.4f) : " + sumClass.Sum(2.3f, 5.4f));  
  31.   
  32.         Console.WriteLine("sum of 2 integers (10, 20, 50) : " + sumClass.Sum(10, 20, 50));  
  33.   
  34.         Console.ReadKey();  
  35.     }  
  36.   
  37. }  
operator overloading :

Operator overloading allows user to redefine the meaning of the operator. Overloaded operators are functions with special names, the keyword operator followed by the symbol for the operator being defined. It has parameters and return value as well like any other method in c# class. 
  1. class ComplexNumber  
  2. {  
  3.     public double RealPart;   
  4.     public double ImaginaryPart;    
  5.   
  6.     public ComplexNumber(double realPart, double imaginaryPart)  
  7.     {  
  8.         RealPart = realPart;  
  9.         ImaginaryPart = imaginaryPart;  
  10.     }  
  11.   
  12.     public ComplexNumber()  
  13.     {  
  14.         RealPart = 0;  
  15.         ImaginaryPart = 0;  
  16.     }  
  17.   
  18.     // Overload + operator to add two Box objects.  
  19.     public static ComplexNumber operator +(ComplexNumber num1, ComplexNumber num2)  
  20.     {  
  21.         ComplexNumber result = new ComplexNumber();  
  22.         result.RealPart = num1.RealPart + num2.RealPart;  
  23.         result.ImaginaryPart = num1.ImaginaryPart + num2.ImaginaryPart;  
  24.         return result;  
  25.     }  
  26.   
  27.     public void Display()  
  28.     {  
  29.         Console.WriteLine("number is : {0} + {1}i", RealPart, ImaginaryPart);  
  30.     }  
  31. }  
  32.   
  33. class MainClass  
  34. {  
  35.     static void Main(string[] args)  
  36.     {  
  37.         ComplexNumber num1 = new ComplexNumber(2, 5);    
  38.         ComplexNumber num2 = new ComplexNumber(3, 7);    
  39.         ComplexNumber num3 = new ComplexNumber();     
  40.   
  41.         // Add two object as follows:  
  42.         num3 = num1 + num2;  
  43.   
  44.         num1.Display();  
  45.         num2.Display();  
  46.         num3.Display();  
  47.   
  48.         Console.ReadKey();  
  49.     }  
  50. }   
Please check Dynamic Polymorphism for dynamic polymorphism and virtual keyword in c#
 
Thanks for reading. 
Ebook Download
View all
Learn
View all