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 pillars in Object Oriented Programming. It allows you to create multiple methods with the same name but different signatures in the same class. The same name methods can also be in derived classes.

There are two types of polymorphism,

  1. Method Overloading
  2. Method Overriding

In this article, I will explain the method overloading and method overriding concept in C#. Furthermore, 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 “Compile Time Polymorphism” or “Static Polymorphism,” and sometimes it is called “Early Binding”.

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

The compiler automatically calls the required method to check the number of parameters and their type passed into that method.

using System;  
namespace DemoCsharp  
{  
    class Program  
    {  
        public int Add(int num1, int num2)  
        {  
            return (num1 + num2);  
        }  
        public int Add(int num1, int num2, int num3)  
        {  
            return (num1 + num2 + num3);  
        }  
        public float Add(float num1, float num2)  
        {  
            return (num1 + num2);  
        }  
        public string Add(string value1, string value2)  
        {  
            return (value1 + " " + value2);  
        }  
        static void Main(string[] args)  
        {  
            Program objProgram = new Program();  
            Console.WriteLine("Add with two int parameter :" + objProgram.Add(3, 2));  
            Console.WriteLine("Add with three int parameter :" + objProgram.Add(3, 2, 8));  
            Console.WriteLine("Add with two float parameter :" + objProgram.Add(3 f, 22 f));  
            Console.WriteLine("Add with two string parameter :" + objProgram.Add("hello", "world"));  
            Console.ReadLine();  
        }  
    }  
}  

In the above example, you can see four methods with the same name, but the type of parameters or several parameters is different. So when you call Add(4,5), the compiler automatically calls the method, which has two integer parameters. On the other hand, when you call Add(“hello”,”world”), the compiler calls the method, which has two string parameters. So basically, in method overloading compiler checks, which method should be called at the compilation time.

Note: Changing the method's return type does not overload the method. You cannot create a method overloaded to vary only by return type.

Method Overriding

Method Overriding is a type of polymorphism. It has several names like “Run Time Polymorphism” or “Dynamic Polymorphism,” and sometimes it is called “Late Binding”. 

Method Overriding means having two methods with the same name and same signatures [parameters]; one should be in the base class, and another method should be in a derived class [child class]. You can override the functionality of a base class method to create the same name method with the same signature in a derived class. You can achieve method overriding using inheritance. Virtual and Override keywords are used to achieve method overriding.

using System;  
namespace DemoCsharp  
{  
    class BaseClass  
    {  
        public virtual int Add(int num1, int num2)  
        {  
            return (num1 + num2);  
        }  
    }  
    class ChildClass: BaseClass  
    {  
        public override int Add(int num1, int num2)  
        {  
            if (num1 <= 0 || num2 <= 0)  
            {  
                Console.WriteLine("Values could not be less than zero or equals to zero");  
                Console.WriteLine("Enter First value : ");  
                num1 = Convert.ToInt32(Console.ReadLine());  
                Console.WriteLine("Enter First value : ");  
                num2 = Convert.ToInt32(Console.ReadLine());  
            }  
            return (num1 + num2);  
        }  
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            BaseClass baseClassObj;  
            baseClassObj = new BaseClass();  
            Console.WriteLine("Base class method Add :" + baseClassObj.Add(-3, 8));  
            baseClassObj = new ChildClass();  
            Console.WriteLine("Child class method Add :" + baseClassObj.Add(-2, 2));  
            Console.ReadLine();  
        }  
    }  
}  

In the above example, I have created two same-name methods in the BaseClass and the ChildClass. When you call the BaseClass Add method with less than zero value as parameters, then it adds successfully. But when you call the ChildClass Add method with less than zero value, it checks for a negative value. If the passing values are negative, it asks for a new value.

So, here it is clear that we can modify the base class methods in derived classes.

Points to be remembered,

  1. The method cannot be private.
  2. Only abstract or virtual methods can be overridden.
  3. Which method should be called is decided at run time.

Conclusion

So, today we learned what polymorphism is in OOP and the differences between method overloading and method overriding.


Similar Articles