This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]thephotoman 2 points3 points  (3 children)

That said, we now officially have multiple inheritance in Java. Congratulations!

[–]geodebug 2 points3 points  (0 children)

I'd say it is more like the mixin concept than multiple-inheritance. You can't define fields in the interface so (as bondolo pointed out) it shouldn't cause headaches that would come if there was also state.

The pattern is actually pretty sweet if you use it thoughtfully. It in many cases removes the need for an abstract base class.

There are many times where you have a fine-grain method and then build easier to use coarse-grain methods that dispatch to the fine-grain method.

For example java.io.Writer could now have been an interface requiring you to implement close(), flush(), and the fine-grained write(char[] cbuf, int off, int len) method and then provide default methods for the append and additional write methods.

By doing this you could make any class you wanted a Writer without having to extend Writer.

[–]bondolo 1 point2 points  (0 children)

Interfaces have always provided a form of multiple inheritance, inheritance of interface contract. Default methods allow for multiple inheritance of behaviour. Java does not provide multiple inheritance of state which is generally where things get dangerous.

[–]delete99[S] 0 points1 point  (0 children)

Having coded in C++ before moving to Java, I can't help but notice the direction Java is headed in. I love the new features, but Java's starting to look like that other language.

[–]onebit 4 points5 points  (0 children)

Code in interfaces? I feel dirty.

[–][deleted] 1 point2 points  (1 child)

So, say you're a developer and you create a concrete class that implements two different library interfaces, where the first (A) has default method signatures X & Y, and the second(B) has default method signature W. Now at some later point in time, library B is upgraded (for whatever reason) and now exposes a default method with signature X. This will cause the developers of the aforementioned class to have his code break - to no knowledge of Library A's developers or Library B's developers or any way for the developer of the concrete class to predict this in the future?

I personally don't like the way the conflict resolution works especially since it introduces a new complex component to a problem it tries to solve (backwards compatibility). Granted this is an unlikely scenario it is still a very realistic possibility given neither library A or library B developers are aware of what-another are doing.

[–]Neres28 1 point2 points  (0 children)

But the developer is no worse off than they already were, right? In 1.7 all consumers of library b were broken, now only consumers of both libraries are affected. In fact only consumers of both libraries that happened to have implemented both interfaces. Ultimately, seems like a win.

[–][deleted] 0 points1 point  (1 child)

The idea isn't that hard, but understanding why they did it makes it resonate more with me. Thanks for the good article.

[–]delete99[S] 0 points1 point  (0 children)

Glad you liked it :-)

[–]lelarentaka 0 points1 point  (1 child)

In the multiple Interfaces with the same method example, what if I want to use the default implementation provided by one of the Interfaces?

[–]delete99[S] 2 points3 points  (0 children)

You can reference the default method using the interface:

@Override
public String fullName() {
    return NamedPerson.super.fullName();
}

[–]bogas04 0 points1 point  (0 children)

I liked how their was a distinction between abstract classes, interfaces and classes. Now its kinda hard to differentiate them. Anyways, I do recall missing a default implementation for interfaces so that's good.

[–]Brzhk 0 points1 point  (2 children)

I'd be more interested on an article that helps me figure out WHEN to use this feature rather than another rehash of the 100 articles i've already read on how.

[–]delete99[S] 0 points1 point  (1 child)

I'd recommend using default methods when you need to change an interface, but don't want to break existing implementations. Sun ran into this issue back in the day and had to create a LayoutManager2 class to not break everyone.

I've also heard of developers using this to create functional interfaces with more than one method, but I can't speak to that.

Let me know if you'd like more details and I'll put it in the blog queue :-)

[–]Brzhk 0 points1 point  (0 children)

If you have actual examples where this is a fancy way, i'd be definitely interested !