by Nezghul · 1y ago · 0 comments
What if instead of Rectangle class we would have ReadonlyRectangle and Rectangle? Square could then inherit from ReadonlyRectangle, so code expecting only to read some properties and not write them could accept Square objects as ReadonlyRectangle. Alternatively if we really want to have only Square and Rectangle classes, there could be some language feature that whenever you want to cast Square to Rectangle it must be "const Rectangle" (const as in C++), so again we would be allowed to only use the "safe" subset of object methods.
Perhaps we could go with even more classes. "Rectangle" and "Square" for the read-only methods, without any implications about mutability. "MutableRectangle" and "MutableSquare" for mutable implementations; "ImmutableRectangle" and "ImmutableSquare" for immutable implementations.
- "Rectangle" has methods "getWidth" and "getHeight".
- "Square" has a method "getSide".
- "ImmutableRectangle" implements "Rectangle".
- "ImmutableSquare" implements "Rectangle" and "ImmutableRectangle" and "Square".
- "MutableRectangle" implements "Rectangle"; has extra methods "setWidth" and "setHeight".
- "MutableSquare" implements "Rectangle" and "Square"; has an extra method "setSide".
...or you could just give up, and declare two classes "Square" and "Rectangle" (mutable) that either have nothing in common, or they just both extend some "Shape" class that can paint them and calculate the area.