Liskov Substitution Principle

noun

say it: “LIS-kof sub-stih-TOO-shun PRIN-suh-pul” /ˈlɪskɔf ˌsʌbstɪˈtuʃən ˈprɪnsəpəl/

The principle that anywhere code expects a parent class, any of its subclasses must work, keeping every promise the parent makes.

What is Liskov Substitution Principle?

The Liskov Substitution Principle says a child class must work anywhere its parent is expected, without surprises. If code works with a Bird, it must still work when handed any kind of bird.

// Breaks the rule: code that calls bird.Fly() crashes for penguins
public class Penguin : Bird
{
    public override void Fly() => throw new NotSupportedException();
}

In a sentence

  • “A pass/fail course that throws when asked for a letter grade breaks the Liskov substitution principle.”

Level: Freelancer · More OOP terms at this level

Updated