Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / SOLID Principles

    L: The Liskov Substitution Principle

    it may not demand more from callers (stricter inputs — e.g. rejecting values the parent accepts);

    Chapter 4 of 84 min read

    The Plain Picture#

    You hire a substitute teacher. You expect them to do what a teacher does — take attendance, teach, set homework. If the substitute refuses to take attendance, or grades everyone "A" regardless, the school breaks even though they're technically "a teacher".

    The Real Name#

    Liskov Substitution Principle (LSP) (Barbara Liskov, 1987): objects of a subclass must be usable anywhere the parent is expected, without breaking the program. A subclass must keep every promise the parent makes:

    • it may not demand more from callers (stricter inputs — e.g. rejecting values the parent accepts);

    • it may not deliver less (weaker results — e.g. returning null where the parent never does);

    • it must preserve the parent's invariants (Object-Oriented Programming Ch 1);

    • it must not throw new kinds of exceptions callers aren't prepared for.

    Under the Hood — the famous example#

    "A square is a rectangle" — true in geometry. So:

    Java
    class Rectangle {
        protected int w, h;
    
        void setWidth(int w) {
            this.w = w;
        }
    
        void setHeight(int h) {
            this.h = h;
        }
    
        int area() {
            return w * h;
        }
    }
    
    class Square extends Rectangle {
        // keeps sides equal
        @Override
        void setWidth(int w) {
            this.w = w;
            this.h = w;
        }
    
        @Override
        void setHeight(int h) {
            this.w = h;
            this.h = h;
        }
    }
    
    static int stretch(Rectangle r) {
        // written against Rectangle's promise:
        r.setWidth(5); // "width and height can be set independently"
        r.setHeight(4);
        return r.area(); // expects 20
    }
    Java
    System.out.println(stretch(new Rectangle()) + " " + stretch(new Square()));
    // 20 16

    Square broke Rectangle's promise that width and height are independent, so correct-looking code gives a wrong answer. "Is-a" in the real world doesn't guarantee is-a in code — what matters is behaviour. The fix: don't make Square a subclass of a mutable Rectangle; give both an interface like Shape { int area(); }, or make them immutable.

    StudyBuddy examples of LSP violations:

    • a ReadOnlyNoteRepository extends NoteRepository whose save() throws UnsupportedOperationException — callers of NoteRepository expect save to work. (Better: split the interface — Ch 5.)

    • a FreeAccount extends Account whose spend() silently does nothing.

    Test for LSP: could a caller who only knows the parent type be surprised by this subclass? If yes, it's a violation — use composition (Object-Oriented Programming Ch 7) or a different interface.

    Check yourself

    3 questions
    1

    State LSP in one sentence.

    Show answer

    A subclass must be usable anywhere its parent is expected without changing the program's correctness.

    2

    Why does stretch(new Square()) return 16?

    Show answer

    Setting the height also changed the width (to 4), breaking the parent's promise that they're independent: 4 × 4 = 16.

    3

    A subclass overrides a method and throws UnsupportedOperationException. Which principle does that usually break, and what's the fix?

    Show answer

    LSP. Split the interface so the class only implements what it can actually do (Ch 5), or use composition.