In SOLID, which principle ensures substitutability of derived classes for base classes without altering correctness?

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

In SOLID, which principle ensures substitutability of derived classes for base classes without altering correctness?

Explanation:
Liskov Substitution Principle is about substitutability: any object of a derived type should be usable wherever a base type is expected, without changing the correctness of the program. In practical terms, a subclass must honor the contract of its base class. It should not introduce stronger preconditions, should preserve postconditions, invariants, and expected exceptional behavior, and it should produce results consistent with what code relying on the base type would anticipate. If a subclass changes how methods behave in a way that breaks those expectations, code that works with the base class can fail when given the subclass. For example, if a base type promises not to return nulls, a subclass that might return null would violate LSP. The other SOLID principles address different concerns: Interface Segregation focuses on small, client-specific interfaces; Dependency Inversion emphasizes depending on abstractions rather than concrete implementations; Open-Closed advocates extending behavior without modifying existing code. Therefore, the principle that ensures substitutability is the Liskov Substitution Principle.

Liskov Substitution Principle is about substitutability: any object of a derived type should be usable wherever a base type is expected, without changing the correctness of the program. In practical terms, a subclass must honor the contract of its base class. It should not introduce stronger preconditions, should preserve postconditions, invariants, and expected exceptional behavior, and it should produce results consistent with what code relying on the base type would anticipate. If a subclass changes how methods behave in a way that breaks those expectations, code that works with the base class can fail when given the subclass. For example, if a base type promises not to return nulls, a subclass that might return null would violate LSP. The other SOLID principles address different concerns: Interface Segregation focuses on small, client-specific interfaces; Dependency Inversion emphasizes depending on abstractions rather than concrete implementations; Open-Closed advocates extending behavior without modifying existing code. Therefore, the principle that ensures substitutability is the Liskov Substitution Principle.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy