โ† Back to OOP Concepts
๐Ÿ”„
Principles

Liskov Substitution

Subtypes must be substitutable for their base types without altering the correctness of the program.

The Problem

If a subclass overrides behavior in ways that violate the expectations of its base class โ€” e.g., throwing for operations the base class supports, or returning unexpected results โ€” code that relies on the base type breaks when given the subtype.

How It Works

  • โ€บA subclass should honor the contracts (preconditions, postconditions, invariants) of its superclass
  • โ€บOverridden methods should accept at least as much input and return at least as little as the base method
  • โ€บClassic violation: Square extends Rectangle but breaks the independence of setWidth/setHeight
  • โ€บIf a subtype can't fully honor the base type's contract, reconsider the inheritance relationship

When to Use

โœ“Designing class hierarchies where subclasses must be drop-in replacements
โœ“Reviewing inheritance to check for surprising overridden behavior
โœ“Choosing between inheritance and composition for an "almost is-a" relationship

Real-World Examples

๐ŸŽ
iOS

Any UIView subclass must work correctly anywhere a UIView is expected

๐Ÿค–
Android

Any RecyclerView.ViewHolder subclass must behave correctly within the adapter

๐ŸŒ
Web

Any Storage implementation (localStorage/sessionStorage) must support the same get/set/remove contract

Code Examples

Swift
// โŒ Violation: Penguin can't fly, breaking the Bird contract
// class Penguin: Bird { override func fly() { fatalError("can't fly") } }

// โœ… Fix: separate flying ability into its own protocol
protocol Bird {
    func eat()
}

protocol FlyingBird: Bird {
    func fly()
}

struct Sparrow: FlyingBird {
    func eat() { print("Sparrow eating") }
    func fly() { print("Sparrow flying") }
}

struct Penguin: Bird {
    func eat() { print("Penguin eating") }
    // no fly() โ€” Penguin is not a FlyingBird, and that's fine
}

func feed(_ bird: Bird) { bird.eat() }
feed(Penguin())   // works correctly for any Bird