Which statement best captures the Liskov Substitution Principle?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

Which statement best captures the Liskov Substitution Principle?

Explanation:
The main idea here is substitutability: any subtype should be usable wherever its base type is expected, without causing unexpected behavior. This means that if a piece of code uses a base class type, you should be able to pass an instance of a subclass and not break the code’s guarantees. The subclass must honor the contract established by the base type—preconditions should not be made stronger, postconditions should remain true, and invariants maintained by the base type should still hold after operations on the subclass. That’s why this option best captures the Liskov Substitution Principle: it emphasizes that subtypes must truly be interchangeable for their base types in practice. If a subclass imposes stricter requirements or changes the expected outcomes, substituting it for the base type would violate the contract and lead to bugs, even if the subclass looks compatible at first glance. The other statements miss the core point. Interface segregation focuses on designing smaller, client-specific interfaces, not on the substitutability of subtypes. The idea that low-level modules shouldn’t be replaced by high-level ones touches on dependency relationships rather than behavioral contracts. And the notion that inheritance should be avoided whenever possible is a broad guideline; LSP is specifically about ensuring that inheritance preserves substitutability and behavior, not about whether to use inheritance at all.

The main idea here is substitutability: any subtype should be usable wherever its base type is expected, without causing unexpected behavior. This means that if a piece of code uses a base class type, you should be able to pass an instance of a subclass and not break the code’s guarantees. The subclass must honor the contract established by the base type—preconditions should not be made stronger, postconditions should remain true, and invariants maintained by the base type should still hold after operations on the subclass.

That’s why this option best captures the Liskov Substitution Principle: it emphasizes that subtypes must truly be interchangeable for their base types in practice. If a subclass imposes stricter requirements or changes the expected outcomes, substituting it for the base type would violate the contract and lead to bugs, even if the subclass looks compatible at first glance.

The other statements miss the core point. Interface segregation focuses on designing smaller, client-specific interfaces, not on the substitutability of subtypes. The idea that low-level modules shouldn’t be replaced by high-level ones touches on dependency relationships rather than behavioral contracts. And the notion that inheritance should be avoided whenever possible is a broad guideline; LSP is specifically about ensuring that inheritance preserves substitutability and behavior, not about whether to use inheritance at all.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy