The Liskov Substitution Principle

New classes should be logical, consistent extensions of their superclasses, but what does it mean to be logical and consistent? A Java compiler will ensure a certain level of consistency, but many principles of consistency will elude a compiler. One principle you should consider in your designs is the Liskov Substitution Principle (LSP). This principle, documented in Liskov (1987), can be paraphrased: An instance of a class should function as an instance of its superclass.

Basic LSP compliance is built into OO languages, such as Java. For example, it is legal to refer to a Rocket object as a Firework, as Rocket is a subclass of Firework:

Firework f = new Rocket();

Some aspects of LSP compliance require human-level ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.