232k views
3 votes
Select the option that best describes the Liskov Substitution Principle.

a. Subclasses should be substitutable for their base classes.
b. Subclasses should override base class methods.
c. Subclasses should inherit private members.
d. Subclasses should avoid polymorphism.

User Cboler
by
7.0k points

1 Answer

5 votes

Final answer:

The Liskov Substitution Principle mandates that subclasses should be substitutable for their base classes, ensuring that inheritance in object-oriented programming reinforces correct program behavior and maintainability. This is essential for writing robust and flexible code.

Step-by-step explanation:

The Liskov Substitution Principle is a core concept in object-oriented programming that states that if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program (correctness, task performed, etc.). This principle is a part of the SOLID principles, which is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible, and maintainable.

The correct answer to the student's question is: a. Subclasses should be substitutable for their base classes. This means that a subclass should not only inherit the behavior of the base class but should also be able to perform without causing errors or incorrect outcomes in the program when it is used in place of the base class.

Violations of this principle may lead to code that is confusing, difficult to maintain and that may break when base classes are used polymorphically. An example can be overriding a base class method in a way that changes the original behavior substantially so that the subclass no longer represents the same concept as the base class.

User Alexandre Ouicher
by
7.8k points