Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liskov's Substitution Principle - How to model Square and Rectangle [duplicate]

Possible Duplicate:
Is deriving square from rectangle a violation of Liskov's Substitution Principle?

By applying the LSP, can anyone give me an implementation of Square and Rectangle?

I have read the book - "Head First Object-Oriented Analysis and Design", they said if Sqaure inherit from Rectangle, it violated the LSP but does not have a proper implementation.

Anyone want a try?

like image 217
Howard Avatar asked Dec 03 '10 18:12

Howard


1 Answers

If you make Square and Rectangle immutable, then you won't violate LSP.

The issue is if you can independently change the width and height of a Rectangle, and a Square is a Rectangle, then you can change a Square to not be a square.

like image 52
Laurence Gonsalves Avatar answered Sep 29 '22 00:09

Laurence Gonsalves