Inheritance Often Doesn't Make Sense
People in this thread are trying to separate the concept of mutation from the concept of inheritance, but the problem with this is that you can&#x27;t separate the two. Consider the following pseudocode:<p><pre><code>    Rectangle r = new Rectangle(height = 3, width = 5);
    Square s_as_s = new Square(side = 4);
    Rectangle s_as_r = s_as_s;

    print(r.height); &#x2F;&#x2F; prints 3
    print(r.width);  &#x2F;&#x2F; prints 5
    print(s_as_s.height); &#x2F;&#x2F; prints 4
    print(s_as_s.width);  &#x2F;&#x2F; prints 4
    print(s_as_r.height); &#x2F;&#x2F; prints 4
    print(s_as_r.width);  &#x2F;&#x2F; prints 4
    print(r is_a? Square); &#x2F;&#x2F; prints false
    print(s_as_s is_a? Square); &#x2F;&#x2F; prints true
    print(s_as_r is_a? Square); &#x2F;&#x2F; prints true
</code></pre>
Okay, so the question comes up when you mutate these results:<p><pre><code>    r.height = 2;
    s_as_r.height = 2;
</code></pre>
Keep in mind, this is a perfectly reasonable thing to do in both cases: you&#x27;re just making two rectangles a little shorter. But no matter how you handle this situation, the results are surprising:<p>One way:<p><pre><code>    print(r.height); &#x2F;&#x2F; prints 2
    print(r.width);  &#x2F;&#x2F; prints 5
    print(s_as_s.height); &#x2F;&#x2F; prints 2
    print(s_as_s.width);  &#x2F;&#x2F; prints 2
    print(s_as_r.height); &#x2F;&#x2F; prints 2
    print(s_as_r.width);  &#x2F;&#x2F; prints 2
    print(r is_a? Square); &#x2F;&#x2F; prints false
    print(s_as_s is_a? Square); &#x2F;&#x2F; prints true
    print(s_as_r is_a? Square); &#x2F;&#x2F; prints true
</code></pre>
This is the simplest to implement, but only because there&#x27;s a part of the contract of Rectangle which is <i>implied</i> and not enforced by the compiler. When we change the height of a rectangle, we don&#x27;t expect the width to change. This is the sort of gotcha that needs to be put in the documentation in big red letters: &quot;WARNING: CHANGING THE HEIGHT MAY CHANGE THE WIDTH IN SOME SITUATIONS.&quot;<p>Another way:<p><pre><code>    print(r.height); &#x2F;&#x2F; prints 2
    print(r.width);  &#x2F;&#x2F; prints 5
    print(s_as_s.height); &#x2F;&#x2F; prints 2
    print(s_as_s.width);  &#x2F;&#x2F; prints 4
    print(s_as_r.height); &#x2F;&#x2F; prints 2
    print(s_as_r.width);  &#x2F;&#x2F; prints 4
    print(r is_a? Square); &#x2F;&#x2F; prints false
    print(s_as_s is_a? Square); &#x2F;&#x2F; prints true
    print(s_as_r is_a? Square); &#x2F;&#x2F; prints true
</code></pre>
But now you&#x27;ve broken the contract of Square: the user is going to be very surprised when changing the side of one side of a Square means that the Square instance no longer represents a square.<p>Okay, what about this:<p><pre><code>    print(r.height); &#x2F;&#x2F; prints 2
    print(r.width);  &#x2F;&#x2F; prints 5
    print(s_as_s.height); &#x2F;&#x2F; prints 2
    print(s_as_s.width);  &#x2F;&#x2F; prints 4
    print(s_as_r.height); &#x2F;&#x2F; prints 2
    print(s_as_r.width);  &#x2F;&#x2F; prints 4
    print(r is_a? Square); &#x2F;&#x2F; prints false
    print(s_as_s is_a? Square); &#x2F;&#x2F; prints false
    print(s_as_r is_a? Square); &#x2F;&#x2F; prints false
</code></pre>
This might be possible with some horrible hack in a language that does dynamic typing. This maintains the contracts of all the types, but I&#x27;d argue that it breaks the contract of the language itself: it&#x27;s deeply confusing to have the type of the s_as_s variable change out from under you.<p>Perhaps you could do this:<p><pre><code>    print(r.height); &#x2F;&#x2F; prints 2
    print(r.width);  &#x2F;&#x2F; prints 5
    print(s_as_s.height); &#x2F;&#x2F; prints 2
    print(s_as_s.width);  &#x2F;&#x2F; prints 2
    print(s_as_r.height); &#x2F;&#x2F; prints 2
    print(s_as_r.width);  &#x2F;&#x2F; prints 4
    print(r is_a? Square); &#x2F;&#x2F; prints false
    print(s_as_s is_a? Square); &#x2F;&#x2F; prints true
    print(s_as_r is_a? Square); &#x2F;&#x2F; prints false
</code></pre>
Setting aside how one might even implement this, we&#x27;ve now got the surprising result that s_as_s and s_as_r seem to be different instances now.<p>Maybe we should have prevent this in the first place:<p><pre><code>    r.height = 2; &#x2F;&#x2F; works
    s_as_s.height = 2; &#x2F;&#x2F; throws CannotSetSideViaHeightException
    s_as_r.height = 2; &#x2F;&#x2F; throws CannotSetSideViaHeightException
</code></pre>
s_as_s.height = 2 throwing an exception sort of makes sense, but now s_as_r isn&#x27;t behaving like a rectangle--we&#x27;re breaking the Rectangle contract again.<p>Another way to prevent it:<p><pre><code>    r.height = 2; &#x2F;&#x2F; works
    s_as_s.height = 2; &#x2F;&#x2F; works
    s_as_r.height = 2; &#x2F;&#x2F; throws ThisWouldBeConfusingException
</code></pre>
Again you&#x27;re breaking the contract of the language, that s_as_s and s_as_r seem to be different objects.<p>There&#x27;s only one way left I can think of to make Rectangle maintain its contract, Square maintain its contract, and keep s_as_s and s_as_r behaving the same way:<p><pre><code>    r.height = 2; &#x2F;&#x2F; throws MutationException
    s_as_s.height = 2; &#x2F;&#x2F; throws MutationException
    s_as_r.height = 2; &#x2F;&#x2F; throws MutationException
</code></pre>
Does this look familiar? It should: it&#x27;s basically immutability implemented as checks at run time, which is not a good way to implement it. At this point we should just implement these as immutable objects.<p>I&#x27;m not necessarily saying that immutability is the only way. You could also give up inheritance:<p><pre><code>    Rectangle makeSquare(int side) {
      return new Rectangle(side, side);
    }

    Rectangle r = new Rectangle(3, 5);
    Rectangle s_as_r = makeSquare(4);

    print(r.isSquare); &#x2F;&#x2F; prints false
    print(s_as_r.isSquare); &#x2F;&#x2F; prints true

    r.height = 2;
    s_as_r.height = 2;

    print(r.height); &#x2F;&#x2F; prints 2
    print(r.width); &#x2F;&#x2F; prints 5
    print(s_as_r.height); &#x2F;&#x2F; prints 2
    print(s_as_r.width); &#x2F;&#x2F; prints 4

    print(r.isSquare); &#x2F;&#x2F; prints false
    print(s_as_r.isSquare); &#x2F;&#x2F; prints true
</code></pre>
This also results in unsurprising behavior.