Suggest an editImprove this articleRefine the answer for “Give an example of an LSP violation”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**An example of an LSP violation** is a situation where a descendant breaks the expectations set by the base class. One of the most well-known examples is the relationship between a rectangle and a square. **Key point:** `Square` cannot substitute for `Rectangle`, even though it formally inherits from it - this is a classic violation of the Liskov Substitution Principle.Shown above the full answer for quick recall.Answer (EN)ImageAn example of an LSP violation is a situation where a descendant **breaks the expectations** set by the base class. One of the most well-known examples is the *rectangle and square* relationship. ### Example: Base class: ```python class Rectangle: def __init__(self, width, height): self.width = width self.height = height def set_width(self, w): self.width = w def set_height(self, h): self.height = h def area(self): return self.width * self.height ``` Subclass: ```python class Square(Rectangle): def set_width(self, w): self.width = w self.height = w # breaks the contract def set_height(self, h): self.width = h self.height = h # breaks the contract ``` ### Why this is an LSP violation: 1. **The subclass changes the meaning of the methods' behavior.** In `Rectangle`, `set_width()` changes only the width. In `Square`, it changes the width *and the height*. Client code does not expect this kind of behavior. 2. **Client code starts working incorrectly.** For example: ```python def resize(rect: Rectangle): rect.set_width(10) rect.set_height(20) return rect.area() ``` Expected behavior for a rectangle: ```javascript area = 10 * 20 = 200 ``` Actual behavior for a square: ```javascript set_width(10) → width=10, height=10 set_height(20) → width=20, height=20 area = 20 * 20 = 400 # incorrect relative to expectations ``` 3. **The subclass forces the client to perform type checks.** ```python if isinstance(rect, Square): ... ``` This is direct proof of an LSP violation. ### Summary `Square` **cannot substitute for** `Rectangle`, even though it formally inherits from it - and this is a classic violation of the Liskov Substitution Principle.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.