On 1 March 2012 14:37, Reinier Zwitserloot <[email protected]> wrote:

> On Thursday, March 1, 2012 12:20:26 PM UTC+1, KWright wrote:
>>
>> I never really had a big problem with String being final, although the
>> claims that it was for security reasons seem a bit weak given that strings
>> are also immutable.
>>
>>
> If String wasn't final, it would obviously not be immutable, duh.
>
>>
>>>
>
Not so, a thing can be both extensible and immutable.  All that's necessary
is for the *members* of an immutable object to be final and for methods to
be pure, you can then happily subclass and add functionality to your
heart's extent.

Some hypothetical SubclassedString won't necessarily be immutable, but it
shouldn't be able to mutate in a way that's visible to any method using it
typed as a String (otherwise you have an LSP violation).  In the case of a
String, it's enough that the backing char[] and a few methods are marked
final.

There's still value in subclassing an immutable object, such as access to
protected fields, or the ability to override a broken hashCode
implementation.

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to