1

Everywhere I could read about Liskov Substitution, only 1 example is available which is Rectangle and Square and It is explained how we can violate it but no corrective actions are described. One such example is available at This link :

I want to know corrective steps If we are violating it.

Thanks in advance.

a.ak
  • 659
  • 2
  • 12
  • 26
Pankaj
  • 121
  • 7
  • related: [Is deriving square from rectangle a violation of Liskov's Substitution Principle?](https://stackoverflow.com/q/1030521/1371329) – jaco0646 Apr 01 '21 at 14:04

1 Answers1

1

In the case of Rectangle and Square the corrective action is simply, don't implement inheritance between them. The LSP tells us that neither is a parent or child of the other, so keep them separate. They can still be siblings, e.g. both can inherit from Shape; but they are separate branches of the Shape hierarchy.

The other potential solution to an LSP violation between two classes is to redefine one or both of them. This isn't a viable solution for Rectangle and Square because they are defined mathematically; but in a scenario where you have control of the abstraction or its implementation, you can edit the contract to fit the code or vice versa.

jaco0646
  • 15,303
  • 7
  • 59
  • 83