Defender methods and compatibility
Brian Goetz
brian.goetz at oracle.com
Sun Nov 28 16:55:36 PST 2010
First, remember that Java already has multiple inheritance -- of types (via interfaces) as well as the contract (methods) for those types. And this is considered one of Java's good features.
The goal is: allow for evolution of types defined by interfaces without breaking existing implementations. An example would be adding a "forEach(lambda)" method to Collection. Currently we could add such a method to Collection, but the cost would be high: breaking all implementation classes (or at least all those that don't extend JDK implementations, such as AbstractCollection.)
We like the existing multiple inheritance in Java; it is far more flexible than the single inheritance of classes. The limitation is that you can't put state or behavior in interfaces, just interface contacts.
Extension methods lifts the restriction about behavior, but not about state. This enables interface evolution, among other things.
Much of the practical trouble with multiple inheritance in languages like C++ stem from state and from non-virtual methods. Java doesn't have the latter. Extension methods does not introduce the former.
It is very easy to say "multiple inheritance bad", but this is a simplistic view of the world. (Most Java developers don't even realize that they are working with multiple inheritance every day.) The goal here is to find a point that admits as much of the good while preventing most of the bad. Obviously there's some room for disagreement on where that line is.
On Nov 28, 2010, at 7:48 PM, Llewellyn Falco wrote:
> sorry, i know this must sound like a newbie question, but I am confused as to the whole point of the default and defender methods for interfaces.
>
> I've been following the thread for a while now, but still can't seem to get my head around it.
>
> As far as I can tell, this allows you to have Interfaces that act like abstract objects.
> Which is confusing, why not just have abstract objects?
>
> the 1 possible difference I can see is this would allow for multiple inheritance.
> and that leads to a very odd question of
>
> "why would you want to bring back multiple inheritance?"
>
>
> is there something I am missing here?
>
> llewellyn
More information about the lambda-dev
mailing list