Overloading gives several methods one name with different parameters, and the compiler picks one at compile time. Overriding replaces a base class method in a derived class, and the runtime picks one based on the object's actual type.
The compiler chooses an overload from the argument types, so overloading is compile-time polymorphism. Overloads can live in the same class, like the two Add methods below. The return type alone cannot tell overloads apart.
Overriding needs virtual or abstract on the base method and override in the derived class. A Shape variable holding a Circle runs Circle.GetArea, so overriding is runtime polymorphism.
Use overloads for the same operation on different inputs. Use overriding to change behavior in a subclass.
// 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
{
private double radius;
public override double GetArea() { return Math.PI * radius * radius; }
}
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓