Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Liskov substitution principle

Принцип подстановки Лисков

Code written for a base type should still work when it receives any subtype. The subtype can use a different implementation, but it must keep the promises that callers rely on.

Also known as: LSP

What LSP means

The Liskov substitution principle (LSP) is the L in SOLID. It says that if code works with a base type, replacing an object of that type with an object of a subtype must not break anything the code is entitled to rely on. This is about behavior, not just whether an assignment compiles. Two classes can implement the same interface and still disagree on what a method call means.

What a subtype has to preserve

Start with the base type’s contract: which calls are valid, what they guarantee, and what changes clients can observe. A subtype can accept more inputs or offer stronger guarantees, but it cannot take away guarantees the base type already makes.

  • Don't strengthen preconditions. If the caller may pass an empty collection, a subtype must not require at least one item.
  • Don't weaken postconditions. If a method promises to return matching records in timestamp order, the subtype must preserve that order. Failure behavior matters too: an implementation cannot throw for an input on which the contract promises success.
  • Preserve invariants and state transitions. If a collection promises to preserve insertion order, it must keep doing so across a sequence of additions and removals, not just for one call in isolation.

Example: a read-only queue

Suppose Queue guarantees that enqueue adds an item to the tail and dequeue removes the head. For a newly created empty queue, a caller can rely on this sequence:

queue.enqueue("first")
queue.enqueue("second")
assert queue.dequeue() == "first"
assert queue.dequeue() == "second"

A ReadOnlyQueue that implements Queue but throws UnsupportedOperationException on enqueue breaks the contract at the first line. Matching the method signature doesn't make it substitutable. A better design is to give read-only consumers a QueueView with operations such as peek, and expose enqueue and dequeue only on the mutable interface. If the original contract explicitly made mutation optional, throwing the documented exception would not by itself violate LSP; callers simply couldn't assume mutation succeeds.

Where it matters and how to explain it in an interview

Apply LSP when swapping implementations behind a shared interface, such as replacing an in-memory repository with one backed by a database. They need not use the same algorithm or have identical performance, but both must honor guarantees such as whether a missing record returns an empty result or raises an error. Running the same behavioral tests against both implementations can catch disagreements that type checking misses.

In an interview, identify a specific promise first. Then show a valid call or sequence that breaks when the subtype is substituted, and propose a design where each implementation can honor its contract. A caller checking the concrete class to work around a broken promise is a useful warning sign; a type check alone is not proof of a violation.