Liskov substitution (LSP) 00:00:00 Make a prediction You will test whether a replacement keeps the original caller's behavioural promises, including boundary inputs and failure outcomes. A file reader returns an empty optional at end of input. A string reader throws an exception there. Both have a method named read. Can a caller swap them safely? No. The old loop expects ordinary exhaustion to produce an empty result. An exception changes observable behaviour even though the method signature is identical. 00:00:35 Noor's substitutes An understudy wears the same costume as the lead actor. The audience still needs the final line delivered at the right cue. A costume that fits cannot compensate for a missing ending. The costume is the method signature; the performance is the behaviour. Check inputs, outputs and promises, including the quiet end of a successful operation. The Liskov substitution principle (LSP) says: if code works with a type, it must also work with every subtype of it, without knowing which one it got. It is the L in SOLID. Polymorphism is built on it. A caller holds a Store and calls save. If some stores silently forget, every caller must ask which store this is, and the benefit of the common type is gone. 00:01:22 Replace a reader without surprising callers A client asks a reader for lines until it returns an empty optional result. It works with a file reader. Substitute a string reader containing the same lines. It must preserve ordering and the end-of-input behaviour promised by the contract. If the replacement throws on normal exhaustion, the calling loop breaks. A common method name is insufficient; contract tests must include ordinary and boundary inputs. Give both readers the same two characters and request one character three times. The results, including end of input, should match the contract. If the file reader returns an empty result while the string reader throws unexpectedly, a caller that depends on substitution now needs concrete-type checks. 00:02:11 Avoid a stronger precondition A payment interface accepts all positive amounts. A replacement that arbitrarily rejects amounts under ten dollars strengthens the precondition. Either expose that limit in the shared contract or give the specialized operation a different role. A caller using the original promise was entitled to pay five dollars. The account contains enough for a five-dollar withdrawal. A subtype introducing a ten-dollar minimum rejects a call that the original contract allowed. Adding a note to that subtype does not protect callers typed against the original promise. Change the advertised capability if the restriction is essential. A replacement that rejects a five-dollar payment strengthens a precondition if its parent accepted every positive amount. Expose a narrower, separately named contract when the two implementations genuinely offer different behaviour. Java: Stack and Vector. java.util.Stack extends Vector, so every stack also has insertElementAt. The documentation says that a more complete and consistent set of stack operations is provided by Deque, which should be used in preference. Java: Properties and Hashtable. Properties is a table of strings but inherits put(Object, Object). The documentation strongly discourages it, and calls an object that contains other things compromised: store and save then fail. 00:03:39 Try an interview follow-up How would you test that all implementations of an interface are substitutable? I would write one contract suite that accepts a factory for the implementation under test. Each promise gets a named test, including edge cases. Running an intentionally broken implementation checks whether the suite can actually detect a violation. Every real implementation then runs the same tests, with the same expected outcomes. A substitute collection returns the same elements but sorts them alphabetically. Can a caller that displays insertion order accept it? Membership alone cannot establish substitutability. Add two elements in reverse alphabetical order and observe iteration. If the contract promises insertion order, this replacement breaks it even though every method signature matches. 00:04:34 What to take with you A subtype may accept more, promise more and offer more. It may not demand more, deliver less, raise surprises or forget what the supertype keeps. Is-a in daily language proves nothing: ask what callers rely on. Most violations are repaired by a smaller promise: split what some cannot do into its own type (Reader beside Store, Shape without setters). Then nobody can pass the wrong object, because it does not compile.