Portfolio

Blog

My articles

Liskov Substitution Principle (LSP):

  • Description: Objects should be replaceable by their subtypes without affecting the correctness of the program.
  • Before LSP:
class Bird {
    public void fly() {
        // Fly
    }
}

class Penguin extends Bird {
    @Override
    public void fly() {
        throw new UnsupportedOperationException("Penguins cannot fly");
    }
}
  • After LSP:
interface Bird {
    void move();
}

class NormalBird implements Bird {
    @Override
    public void move() {
        // Fly
    }
}

class Penguin implements Bird {
    @Override
    public void move() {
        // Penguins walk, they don't fly
    }
}

Web Developer

© Michał Pieróg. All Rights Reserved.

Scroll to Top