Suggest an editImprove this articleRefine the answer for “How do you check whether a class follows the Liskov Substitution Principle?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**You can check whether the Liskov Substitution Principle is followed** by analyzing whether the subclass's behavior violates the expectations set by the base class. **Key point:** a class follows LSP if its instance can be substituted into any code that expects the base class, and the program keeps correct behavior, without additional checks or changes to the logic.Shown above the full answer for quick recall.Answer (EN)ImageYou can check whether the Liskov Substitution Principle is followed by analyzing **whether the subclass's behavior violates the expectations set by the base class**. In effect, you need to make sure the descendant does not change the parent's contract. Criteria to check: ## 1. The subclass should not tighten input conditions (preconditions) If the base class accepts any values within a defined range, the descendant cannot introduce additional restrictions. For example, if a base method accepts any number, the descendant should not require the number to be positive only. ## 2. The subclass should not weaken output conditions (postconditions) If the base method guarantees a certain result (type, format, range), the descendant cannot give weaker guarantees. In other words, it must fulfill everything the parent promised. ## 3. The base class's invariants must be preserved If the base class guarantees the stability of its fields or states, the descendant should not break those rules. For example, if the parent guarantees that an object always stays "valid", the descendant cannot introduce states in which the object becomes "invalid". ## 4. Subclass methods should not throw new exceptions that the base class did not have If a base method cannot throw an error for certain inputs, the descendant has no right to break this rule. Otherwise, client code starts depending on the specific type of the object - this is an LSP violation. ## 5. The subclass should not change the meaning of the base class's methods The main rule: the behavior of a descendant's method must be compatible in meaning with the behavior of the parent's. For example, if the parent's method "adds an element", the descendant should not "replace an element" or "ignore the addition". ## 6. When substituting the descendant for the parent, client code should work without type checks If constructs like these appear somewhere in the code: - `if isinstance(obj, Child): ...` - `if obj.__class__ == Child:` then the descendant has violated LSP, and the client is forced to account for how it works. ## 7. The subclass's behavior should not require additional information that the base class does not require If a base method works under any conditions, the descendant should not introduce a dependency on additional state or context. Summary: A class follows LSP if **its instance can be substituted into any code that expects the base class, and the program keeps correct behavior, without additional checks or changes to the logic**.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.