intermediateLiskov Substitution Principle

What is the difference between structural compatibility and behavioral compatibility when substituting a subclass for its parent?

Structural compatibility means a subclass has all the same method signatures as its parent, so the code compiles when the subclass is used anywhere the parent is expected. Behavioral compatibility means the subclass actually honors the meaning and guarantees behind those methods — the same preconditions, postconditions, and side effects a caller could rely on from the parent. A class like ReadOnlyFile extending File and overriding write() to throw an exception is structurally compatible, since it satisfies the compiler completely, but it is not behaviorally compatible, because callers were implicitly promised that write() was safe to call. LSP specifically requires behavioral compatibility, which is why it is described as a semantic principle rather than a purely structural one — passing the compiler is necessary but never sufficient for a subclass to be a genuinely safe substitute.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to How is the Interface Segregation Principle different from the Single Responsibility Principle?← Back to all Low-Level Design & Design Patterns questions