I am new to design and learning the design principles.
It says deriving square from rectangle is a classic example of violation of Liskov’s Substitution Principle.
If that’s the case, what should be the correct design?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I believe the reasoning is something like this:
Let’s say you have a method that accepts a rectangle and adjusts its width:
It should be perfectly reasonable, given what a rectangle is, to assume that this test would pass:
… because changing a rectangle’s width does not affect its height.
However, let’s say you’ve derived a new Square class from Rectangle. By definition, a square has height and width always equal. Let’s try that test again:
That test will fail, because setting a square’s width to 100 will also change its height.
Thus, Liskov’s substitution principle is violated by deriving Square from Rectangle.
The “is-a” rule makes sense in the “real world” (a square is definitely a kind of rectangle), but not always in the world of software design.
Edit
To answer your question, the correct design should probably be that both Rectangle and Square derive from a common “Polygon” or “Shape” class, which does not enforce any rules regarding width or height.