Polymorphism Part 1- Method Overloading In C#

Method Overloading is one of the types of polymorphism. In the object-oriented programming paradigm, polymorphism often refers to the ability to present the same interface for different forms. Although the concept of polymorphism is similar in all the programming languages, its implementation differs from language to language.

Method overloading is sometimes called static polymorphism or Compile Time Polymorphism or Early Binding. It basically means creating multiple methods in the same class with the same name but with different signatures. Here, different signatures refer to the total number of parameters passed to the functions & type.

Programming Example

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Overloading {  
  7.     class Car {  
  8.         public void Speed(int speed) {  
  9.             Console.WriteLine("Int: " + speed);  
  10.         }  
  11.         public void Speed(long speed) {  
  12.             Console.WriteLine("long: " + speed);  
  13.         }  
  14.         public void Speed(float speed) {  
  15.             Console.WriteLine("float: " + speed);  
  16.         }  
  17.         public void Speed(double speed) {  
  18.             Console.WriteLine("double: " + speed);  
  19.         }  
  20.     }  
  21.     class Program {  
  22.         static void Main(string[] args) {  
  23.             Car c = new Car();  
  24.             c.Speed(100);  
  25.             c.Speed(200 L);  
  26.             c.Speed(212.2 f);  
  27.             c.Speed(313.33);  
  28.         }  
  29.     }  
  30. }  

Output

Int: 100
long: 200
float: 212.2
double : 313.33

In the above example, you can see that there are four methods with the same name but the type of parameter or number of parameters is different. So, during its compilation time the compiler will check which method should be called.

Sometimes, we come across some tricky questions in an interview. I also faced one question about method overloading. The question was like the below one.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Overloading {  
  7.     class Car {  
  8.         public void Speed(int speed) {  
  9.             Console.WriteLine("Int: " + speed);  
  10.         }  
  11.         public void Speed(long speed) {  
  12.             Console.WriteLine("long: " + speed);  
  13.         }  
  14.     }  
  15.     class Program {  
  16.         static void Main(string[] args) {  
  17.             Car c = new Car();  
  18.             c.Speed(100);  
  19.             c.Speed(2147483647);  
  20.             c.Speed(2147483647 L);  
  21.             c.Speed(2147483648);  
  22.         }  
  23.     }  
  24. }  

Output

Int: 100
Int: 2147483647
long: 2147483647
long: 2147483648

From the above output, we can see that for the last value 2147483648, long function is called though it resembles the integer data type. It’s because Int32.MaxValue is equal to 2147483647 and so, instead of passing the value as integer data type, it gets passed as long data type.

Conclusion
 
This blog explained the concept of method overloading. I hope you liked it :). If you have any queries, please comment.
 
Thanks !!!!!