Which statement best captures the Liskov Substitution Principle?

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

Which statement best captures the Liskov Substitution Principle?

Explanation:
Subtypes must be substitutable for their base types. In practice, this means any code that uses a base class should be able to work with an instance of a derived class without knowing it’s been replaced, and without observing different or unexpected behavior. The derived class must honor the contract the base class promises: it should respect the same preconditions, deliver the same postconditions, and maintain the invariants that the base type guarantees. If a subclass strengthens a precondition or weakens a postcondition, or violates an invariant, code that depended on the base type could fail when given the subclass, even though it compiles and runs. This smooth substitutability is what lets you extend behavior through inheritance without breaking existing clients. The other statements miss that focus on substitutability and contract preservation, which is why they don’t capture what the principle is about.

Subtypes must be substitutable for their base types. In practice, this means any code that uses a base class should be able to work with an instance of a derived class without knowing it’s been replaced, and without observing different or unexpected behavior. The derived class must honor the contract the base class promises: it should respect the same preconditions, deliver the same postconditions, and maintain the invariants that the base type guarantees. If a subclass strengthens a precondition or weakens a postcondition, or violates an invariant, code that depended on the base type could fail when given the subclass, even though it compiles and runs. This smooth substitutability is what lets you extend behavior through inheritance without breaking existing clients. The other statements miss that focus on substitutability and contract preservation, which is why they don’t capture what the principle is about.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy