logoalt Hacker News

Nezghul01/22/20252 repliesview on HN

> class Square : Rectangle() { ...

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.


Replies

cryptonector01/22/2025

I think what you mean is that if a Square that is also a Rectangle can't be made to be non-square, then inheritance works. Which, fair enough, but I think there's still other good reasons that inheritance is a bad approach. Interfaces (and traits) are still way better.

Viliam123401/22/2025

What is "ReadonlyRectangle"? Is it just an interface that only exposes read-only methods; or is it an explicit promise that the rectangle is immutable?

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.