In Focus

Polymorphism in C#

In this article I will explain you about method overloading i.e. compile time polymorphism.

Polymorphism in C# 
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is one of the fundamental concepts of OOP.
Polymorphism provides following features: 
  • It allows you to invoke methods of derived class through base class reference during runtime.
  • It has the ability for classes to provide different implementations of methods that are called through the same name.
Polymorphism is of two types: 
  1. Compile time polymorphism/Overloading
  2. Runtime polymorphism/Overriding

Compile Time Polymorphism

Compile time polymorphism is method and operators overloading. It is also called early binding.
In method overloading method performs the different task at the different input parameters.

Runtime Time Polymorphism

Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.
When overriding a method, you change the behavior of the method for the derived class.  Overloading a method simply involves having another method with the same prototype.
Caution: Don't confused method overloading with method overriding, they are different, unrelated concepts. But they sound similar.
Method overloading has nothing to do with inheritance or virtual methods.
Following are examples of methods having different overloads:
  1. void area(int side);  
  2. void area(int l, int b);  
  3. void area(float radius);  
Practical example of Method Overloading (Compile Time Polymorphism)
  1. using System;  
  2. namespace method_overloading  
  3. {  
  4.     class Program  
  5.     {  
  6.         public class Print  
  7.         {  
  8.             public void display(string name)  
  9.             {  
  10.                 Console.WriteLine("Your name is : " + name);  
  11.             }  
  12.             public void display(int age, float marks)  
  13.             {  
  14.                 Console.WriteLine("Your age is : " + age);  
  15.                 Console.WriteLine("Your marks are :" + marks);  
  16.             }  
  17.         }  
  18.         static void Main(string[] args)  
  19.         {  
  20.             Print obj = new Print();  
  21.             obj.display("George");  
  22.             obj.display(34, 76.50f);  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Note: In the code if you observe display method is called two times. Display method will work according to the number of parameters and type of parameters.
When and why to use method overloading
Use method overloading in situation where you want a class to be able to do something, but there is more than one possibility for what information is supplied to the method that carries out the task.
You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing.
Method Overloading showing many forms
  1. using System;  
  2. namespace method_overloading_polymorphism  
  3. {  
  4.     class Program  
  5.     {  
  6.         public class Shape  
  7.         {  
  8.             public void Area(float r)  
  9.             {  
  10.                 float a = (float)3.14 * r;  
  11.                 // here we have used funtion overload with 1 parameter.  
  12.                 Console.WriteLine("Area of a circle: {0}",a);  
  13.             }  
  14.             public void Area(float l, float b)  
  15.             {  
  16.                 float x = (float)l* b;  
  17.                 // here we have used funtion overload with 2 parameters.  
  18.                 Console.WriteLine("Area of a rectangle: {0}",x);  
  19.             }  
  20.             public void Area(float a, float b, float c)  
  21.             {  
  22.                 float s = (float)(a*b*c)/2;  
  23.                 // here we have used funtion overload with 3 parameters.  
  24.                 Console.WriteLine("Area of a circle: {0}", s);  
  25.             }  
  26.         }  
  27.         static void Main(string[] args)  
  28.         {  
  29.             Shape ob = new Shape();  
  30.             ob.Area(2.0f);  
  31.             ob.Area(20.0f,30.0f);  
  32.             ob.Area(2.0f,3.0f,4.0f);  
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  
Things to keep in mind while method overloading
If you use overload for method, there are couple of restrictions that the compiler imposes.
The rule is that overloads must be different in their signature, which means the name and the number and type of parameters.
There is no limit to how many overload of a method you can have. You simply declare them in a class, just as if they were different methods that happened to have the same name.
You can read my other article on inheritance explaining method overriding.
Hope the article would have helped you in understanding polymorphism. Please feel free to contact me for feedback or comments you may have about this article.