liveBook · Manning

Liskov Substitution Principle

As explained in Object-Oriented Software Design in C++

The Liskov Substitution Principle states that objects of a subclass should be usable wherever objects of its superclass are expected without violating correctness.

The Liskov Substitution Principle requires every subclass object to replace its superclass object without causing logic or runtime errors. Different but meaningful results remain acceptable. section 7.2

Apply the principle by checking that:

  • The subclass genuinely fits the superclass’s is-a relationship. section 7.2
  • Overriding functions preserve compatible preconditions and postconditions. section 7.0 section 7.6
  • Substitution does not expose operations that contradict the superclass abstraction. section 7.2

CircularBuffer and vector

Making CircularBuffer a vector subclass exposes at() and erase(), allowing clients to modify or remove elements in ways that violate circular-buffer behavior and eventually cause an exception. Refactoring CircularBuffer to contain an aggregated vector preserves the buffer interface and prevents those invalid substitutions. section 7.2 section 7.6

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains substitution, overriding contracts, and a failing CircularBuffer implementation before refactoring toward aggregation.
Examples
Uses GeneralTax and LocalTax substitution, then contrasts vector-inherited and vector-contained CircularBuffer implementations.
What it assumes
Assumes basic object-oriented inheritance and introduces substitution rules alongside is-a and has-a relationships.
What you can do after
Evaluate subclass designs, preserve overriding contracts, and refactor invalid inheritance toward composition or aggregation.
How it connects
Connects inheritance with overriding, is-a relationships, aggregation, composition, and Programming by Contract.

Also explained in

Compare how 5 books explain Liskov Substitution Principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.