Roadmap › OOP Concepts in C# › Module: The Four Pillars
Lessons in this course (15)
8. Polymorphism: overloading vs overriding
Basic★ Must knowLesson 8 of 15
Compile-time (static) polymorphism — method overloading: same name, different parameter lists, chosen by the compiler. Operator overloading is also compile-time.
Runtime (dynamic) polymorphism — method overriding: a virtual/abstract base method replaced with override in a derived class; the actual object type decides at runtime.
csharp
public class Calculator
{
public int Add(int a, int b) => a + b; // overloads
public double Add(double a, double b) => a + b;
public int Add(int a, int b, int c) => a + b + c;
}
List<Employee> staff = [new Permanent("Asha", 1_200_000), new Contractor("Ravi", 900, 160)];
foreach (var e in staff)
Console.WriteLine(e.Describe()); // each object's own override runsMethod hiding (new) is different: the method chosen depends on the variable's type, not the object's.
csharp
class A { public virtual string Who() => "A"; public string Name() => "A"; }
class B : A { public override string Who() => "B"; public new string Name() => "B"; }
A x = new B();
Console.WriteLine(x.Who()); // B (override — runtime type)
Console.WriteLine(x.Name()); // A (hiding — compile-time type)