Explain method overriding vs method overloading.

Beginner

Answer

Method Overloading:

  • Multiple methods with same name but different parameters
  • Compile-time polymorphism
  • Same class can have overloaded methods
    Method Overriding:
  • Redefining a method in derived class
  • Runtime polymorphism
  • Requires virtual in base class and override in derived class
// Overloading
public class Calculator
{
    public int Add(int a, int b) { return a + b; }
    public double Add(double a, double b) { return a + b; }
}
// Overriding
public class Shape
{
    public virtual double GetArea() { return 0; }
}
public class Circle : Shape
{
    public override double GetArea() { return Math.PI * radius * radius; }
}