defender methods and method of Object

David Conrad drconrad at
Mon Mar 12 05:17:03 PDT 2012

On Fri, Mar 9, 2012 at  8:23 PM,  "Brian Goetz" <brian.goetz at
> wrote:

> I'm not convinced.  The implementations of equals, hashCode, and
> toString provided by an interface are almost certainly going to be wrong
> for most subclasses that inherit them, whereas the versions provided by
> Object are generally sound, if basic.  (To say nothing of the pain that
> will ensue when someone adds a hashCode/equals method pair to an
> interface implemented by a class that doesn't provide such a pair (which
> is a reasonable choice), and all of a sudden, behavior of that class
> when put into hash-based containers changes wildly.)
This is reminiscent of the argument surrounding closing over mutable state
in lambdas. There is a great danger, even a great temptation for a user to
write an accumulator that is unsound. It is, if you will, an attractive

I don't believe that reasoning holds here. If I understand the issue
correctly, unless the methods of Object are special cased, it will not be
possible to provide default implementations of them in an interface. (Or,
it will be possible--just--but unwieldy?)

The mere fact that it is possible to provide an incorrect or unwanted
implementation doesn't seem to me like a good argument for not special
casing them. Of course it's possible to write broken code, but merely
allowing for the possibility of providing implementations doesn't encourage
anyone to do so. In fact, it seems rather unlikely to me. Why would the
author of interface Frobbable even be thinking about overriding toString,
equals, or hashCode?

On the other hand, for those use cases where it does make sense to provide
implementations (on collection classes, as several people have pointed
out), it will not even be possible to do so unless steps are taken to
enable it.

On the gripping hand, I think if it's impossible (or unwieldy) to provide
default implementations of these methods it may violate the principle of
least astonishment. "Oh, I can provide a default implementation of any
method under the sun but not of toString?"

My $0.02,
David Conrad

* An attractive nuisance is something like a swimming pool or trampoline
that is likely to lure neighborhood children into trespassing on someone's
property and injure themselves. It's like a sumptuous meal in fairyland --
it tempts you to your doom.

More information about the lambda-dev mailing list