mediumConceptMidSeniorStaff
Give me a subclass that the compiler accepts but that still breaks the Liskov substitution principle. What rule does it break?
5 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup
Answer
Short answer
Substitutability is a claim about behaviour, and signatures carry none of it.A subclass that rejects an argument its supertype accepted has strengthened a precondition, which the compiler cannot see: the code type-checks, and it breaks callers that were written correctly against the base type.
Was this answer helpful?
Likely follow-ups
- How do you choose between widening the supertype's contract and abandoning the inheritance relationship?
- Why does adding a field and overriding equals break symmetry, and what are the two ways out of it?
- Java's arrays are covariant. Where does that surface at runtime, and what did generics do differently?
- What would a contract test suite for an interface with optional operations have to assert?
Related questions
- How do you design the error contract for an API that other teams have to program against?hardAlso on api-design and contracts6 min
- Where does encapsulation or polymorphism change a design, and how do you decide between composition and inheritance?mediumAlso on polymorphism and inheritance6 min
- What are the four pillars of OOP, and what is the difference between abstraction and encapsulation?easyAlso on polymorphism and inheritance5 min
- Two clients open the same record, both edit it, and the second save silently overwrites the first. How would you use ETags to turn that lost update into something the client can see and handle?mediumAlso on api-design4 min