What is difference between overloading and overriding in C#?


Difference between overloading and overriding.

Overloading :

  • It means methods are declared with same name but different parameters (signatures).
  • It is also called compile time and early binding.
  • Example :-

    public class Overloading
      {
        public int CalculateAmt(int a, int b)  //Method with two int type Parameters.
        {
            return a + b;
        }
    
        public int CalculateAmt(int a, int b, int c)  //Same method name with different parameters.
        {
            return a * b * c;
        }
      }
    

Overriding :

  • It means methods are declared with same name and same parameters (signatures).
  • it is also called run time and late binding.
  • Method overriding is only possible in derived class.
  • Method overriding is not possible in the same class.
  • Only virtual and abstract methods in the base class can be overrides in derived class.
  • Example :

    //Base Class
    public class Math
      {
        public virtual int CalculateAmt(int a, int b)
        {
            return a + b;
        }
      }
    
    //Derived Class
    public class Calculator:Math
    {
        public override int CalculateAmt(int a, int b)
        {
           return a * b;
        }
    }