Liskov Substitution Principle

Objects of a subclass should be usable wherever objects of their parent class are expected without altering the correctness of the program. Subclass methods must accept at least as broad parameter types and return at least as specific types as the parent, and must not strengthen preconditions or weaken postconditions.

Examples:

  • A method feed(Cat c) works with any cat. A subclass that overrides it as feed(Animal c) is safe — it's more permissive. A subclass that restricts it to feed(BengalCat c) violates LSP: passing a generic cat from client code now breaks the method, because the subclass is not a true substitute for its parent.

Synonyms: lsp