3 ms·
Agreed. In fact I would expect the Square subclass to implement width= and height= so that they each actually set both internal values.
by skywhopper 9y ago
Agreed. In fact I would expect the Square subclass to implement width= and height= so that they each actually set both internal values.
- niklasjansson 9y agoSo this behavior is reasonable? Rectangle r=getRectangle(); r.width=1; Assert(1, r.width); //passes r.height=2; Assert(1, r.width); //fails or passes depending on subclass
- Paul_Dirac 9y agoThe problem is that the square is a rectangle in the mathematical sense: a rectangle with ANY additional constraints while code which uses a rectangle expects a rectangle with NO further constraints. I believe the right choice is to have an AbstractRectangle class representing the rectangle with any further constraints and Rectangle class representing the rectangle with no further constraints.
- jdmichal 9y agoAs niklasjansson points out, this solution breaks Liskov substitution. Squares programmed in such a way are no longer substitutable for rectangles. Breaking Liskov substitution is a great way to hide bugs and make your codebase extremely hard to reason about, due to the fact that every subtype can have drastically different behavior than what's defined by the parent type.
- deleted 9y ago[deleted]