[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: the benefits of immutability




Vadim Nasardinov <el-vadimo@comcast.net> writes:
> Perry E. Metzger wrote:
>  > > The finality of the String class is a consequence of its
>  > > immutability.
>  >
>  > That's just plain incorrect. Obvious evidence is that there is also
>  > a mutable string type and it is also final -- i.e. cannot be
>  > subclassed. Just because the strings themselves are immutable
>  > doesn't mean that you can't safely add extra methods etc. to a
>  > subclass. The two are unconnected.
> 
> Generally speaking, it can be pretty hard to safely add methods to a
> subclass without violating the Liskov substitutability principle.

Then we should abandon object oriented programming. In a language like
Java, most of the point of object oriented programming is
subclassing. Otherwise, modules and first class functions give you
everything you could want.

Of course, most of Java doesn't just permit inheritance but indeed
depends on it -- see, for example, all the standard interfaces which
would be meaningless without inheritance because they would have no
implementations. If you seriously argue that forbidding subclassing of
String has something to do with the Liskov substitutability principle,
then you have some explaining to do about the entire rest of Java.

I would therefore guess that this new reason you bring up is not the
reason for making String final, either.

> The problem is not that you can't add methods safely.

Well, then, again, if inheritance is unsafe, why does the whole rest
of the language permit it and indeed depend on it?

In any case, you could have the grace to admit your initial argument
which you've silently abandoned was incorrect. To remind everyone, you
claimed that String had to be final because it is immutable and that
there was that there was a non-final mutable string class. Of course,
String could be non-final and yet immutable, and StringBuffer is also
final.


-- 
Perry E. Metzger		perry@piermont.com