Which SOLID principle is violated when a subclass inherits a method it cannot use?

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 SOLID principle is violated when a subclass inherits a method it cannot use?

Explanation:
It’s about substitutability. The Liskov Substitution Principle says you should be able to replace a base class object with a derived class object without changing the correctness of the program. If a subclass inherits a method it cannot actually support, code that uses the base class interface may rely on that method behaving in a certain way, and substituting the subclass could lead to incorrect results or runtime errors. That breaks the expectation that a derived class can stand in for the base class, which is precisely what Liskov Substitution Principle protects. The other SOLID principles address different concerns: Interface Segregation focuses on keeping interfaces small and tailored to clients; Dependency Inversion centers on depending on abstractions rather than concrete classes; Open/Closed emphasizes extending behavior without modifying existing code.

It’s about substitutability. The Liskov Substitution Principle says you should be able to replace a base class object with a derived class object without changing the correctness of the program. If a subclass inherits a method it cannot actually support, code that uses the base class interface may rely on that method behaving in a certain way, and substituting the subclass could lead to incorrect results or runtime errors. That breaks the expectation that a derived class can stand in for the base class, which is precisely what Liskov Substitution Principle protects. The other SOLID principles address different concerns: Interface Segregation focuses on keeping interfaces small and tailored to clients; Dependency Inversion centers on depending on abstractions rather than concrete classes; Open/Closed emphasizes extending behavior without modifying existing code.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy