The Liskov Substitution Principle (LSP) is a powerful tool to ensure compatibility of subclasses with superclasses. When applied, it leads to more correct inheritance hierarchies that are easy to understand and extend. In its simplest form, the LSP states that an object of a subclass can be supplied wherever an object of its superclass is expected. However, the LSP sometimes prevents legitimate inheritance hierarchies. We show that a number of real-world class derivations don't satisfy the LSP. This article discusses the implications of this general principle on the different parts of a class definition. It pinpoints the language mechanisms that conflict with this principle. This article will try to answer the question whether or not the LSP is an adequate tool to guarantee good class hierarchies. It further presents some recommendations on how this principle could be best supported by an object-oriented language. For explanatory purposes, we will use both the Java and the Eiffel programming languages.
展开▼