Method Overloading And Method Overriding In C#

Polymorphism means “Many Forms”. In Polymorphism, poly means “Many” and morph means “Forms” Polymorphism is one of the main pillar in Object Oriented Programming. You can create multiple methods with same name but different signature in same class or derived class for modifying the functionality of base class. It provides different implementation of method that is implemented with same name.

There are two types of Polymorphism:

  1. Method Overloading
  2. Method Overriding

In this article, I will explain about method overloading and method overriding concept in C#. I will try to demonstrate step by step differences between these.

Method Overloading

Method Overloading
is a type of polymorphism. It has several names like sometime you say “Compile Time Polymorphism” or “Static Polymorphism” and sometimes it is called “Early Binding”. So, all are same with different name.

Method Overloading means creating multiple methods in the class having same name but different signatures (Parameters). It permits a class, struct, or interface to declare multiple methods with the same name with unique signatures.

Compiler automatically calls required method to check number of parameters and their type which are passed into that method.

  1. using System;  
  2. namespace DemoCsharp  
  3. {  
  4.     class Program  
  5.     {  
  6.         public int Add(int num1, int num2)  
  7.         {  
  8.             return (num1 + num2);  
  9.         }  
  10.         public int Add(int num1, int num2, int num3)  
  11.         {  
  12.             return (num1 + num2 + num3);  
  13.         }  
  14.         public float Add(float num1, float num2)  
  15.         {  
  16.             return (num1 + num2);  
  17.         }  
  18.         public string Add(string value1, string value2)  
  19.         {  
  20.             return (value1 + " " + value2);  
  21.         }  
  22.         static void Main(string[] args)  
  23.         {  
  24.             Program objProgram = new Program();  
  25.             Console.WriteLine("Add with two int parameter :" + objProgram.Add(3, 2));  
  26.             Console.WriteLine("Add with three int parameter :" + objProgram.Add(3, 2, 8));  
  27.             Console.WriteLine("Add with two float parameter :" + objProgram.Add(3 f, 22 f));  
  28.             Console.WriteLine("Add with two string parameter :" + objProgram.Add("hello""world"));  
  29.             Console.ReadLine();  
  30.         }  
  31.     }  
  32. }  
In the above example, you can see that there are four methods with same name but type of parameter or number of parameter is different.
When you call Add(4,5), complier automatically call the method which has two integer parameters and when you call Add(“hello”,”world”), complier call the method which has two string parameters. So basically in method overloading complier check which method should be called at the time of compilation.

Note: Changing the return type of method does not make the method overloaded. You cannot create method overloaded vary only by return type.

Method Overriding

Method Overriding
is a type of polymorphism. It has several names like sometime you say “Run Time Polymorphism” or “Dynamic Polymorphism” and sometime it is called “Late Binding”. So, all are same with different name.

Method Overriding means having two methods with same name and same signature [parameters], one should be in base class and other method should be in derived class [child class]. You can override the functionality of base class to create a same name method with same signature in derived class. You can achieve method overriding using inheritance. Virtual and Override keywords are used to achieve method overriding.
  1. using System;  
  2. namespace DemoCsharp  
  3. {  
  4.     class BaseClass  
  5.     {  
  6.         public virtual int Add(int num1, int num2)  
  7.         {  
  8.             return (num1 + num2);  
  9.         }  
  10.     }  
  11.     class ChildClass: BaseClass  
  12.     {  
  13.         public override int Add(int num1, int num2)  
  14.         {  
  15.             if (num1 <= 0 || num2 <= 0)  
  16.             {  
  17.                 Console.WriteLine("Values could not be less than zero or equals to zero");  
  18.                 Console.WriteLine("Enter First value : ");  
  19.                 num1 = Convert.ToInt32(Console.ReadLine());  
  20.                 Console.WriteLine("Enter First value : ");  
  21.                 num2 = Convert.ToInt32(Console.ReadLine());  
  22.             }  
  23.             return (num1 + num2);  
  24.         }  
  25.     }  
  26.     class Program  
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             BaseClass baseClassObj;  
  31.             baseClassObj = new BaseClass();  
  32.             Console.WriteLine("Base class method Add :" + baseClassObj.Add(-3, 8));  
  33.             baseClassObj = new ChildClass();  
  34.             Console.WriteLine("Child class method Add :" + baseClassObj.Add(-2, 2));  
  35.             Console.ReadLine();  
  36.         }  
  37.     }  
  38. }  
In the above example, I have created two same name methods in BaseClass as well as ChildClass. When you call the BaseClass Add method with less than zero value as parameter then it adds successfully. But when you call ChildClass Add method with less than zero value then it checks for negative value. And the passing values are negative then it asks for new value.

So, here it is clear that we can modify the base class method in derived or child class as per our requirement.

Points to be remembered  
  1. Method cannot be private.
  2. Only abstract or virtual method can be overridden.
  3. Which method should be called, it decides on run time.

Conclusion

So, today we learned what Polymorphism is in oops and what are the differences between method overloading and method overriding.

Up Next
    Ebook Download
    View all
    Learn
    View all