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

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 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