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.