← All Quizzes
What is the Liskov Substitution Principle (LSP)?
Explanation
The Liskov Substitution Principle (LSP), the L in SOLID, states that objects of a subclass should be substitutable for objects of the superclass without altering the correctness of the program. Subtypes must honor the contract of the base type: they must not strengthen preconditions, weaken postconditions, or throw new exception types. Classic violations include a Square class extending Rectangle (changing width/height behavior) or a ReadOnlyList extending List (throwing on add). LSP ensures polymorphism works correctly.