Introduction

Once I saw that Java 8 was released, I decided it was time to start looking in depth at the new enhancements to the language. I admit, I didn’t follow this release as closely as I did 7, so the only enhancement I knew of was lambda expressions. As I read through the documentation, and wrote some sample programs to make sure I had a good understanding of the new features, one feature in particular caused a bit of alarm: Default Methods. First, let’s take a look at what they are.

At a glance

Essentially, a default method is a method within an interface that has an implementation. Traditionally interfaces are simply composed of abstract methods and public static final variables. Consider the following:

public interface ExampleInterface {
    public void exampleMethod();
    default public void exampleDefaultMethod() {
        // Some implementation code here
    }
}

If a class decides to implement ExampleInterface, it only needs to implement exampleMethod, and it can simply take the default implementation for exampleDefaultMethod. Now let’s take a look at how Oracle intends for us to use them.

How they should be used

The idea is not that you design your solution with default methods in mind but rather they are an afterthought. On the Oracle trail they state the intent is to “ensure binary compatibility with code written for older versions of those interfaces”(1). Essentially, you and your developers have written a multitude of classes that implement some interface, but you want to add an additional piece.  However, doing this will mean that rebuilding all of those classes that implement this interface will now fail unless you write an implementation for each of those classes. The idea of a default method is that you can add this additional requirement without changing the existing implementations and without modifying them either.

But let’s take a step back. There are a few problems right off the bat. First, let’s accept the fact that sometimes high level changes (such as changes to your interfaces) are going to happen. However, let’s not forget what the purpose of interfaces are. Ideally an interface captures some high level object, or if you’re creating an API than some specific piece of functionality. If some classes only need to implement part of the interface’s functionality, than is that really a good design? Isn’t that what abstract classes were designed for?

Second, this brings in the need to deal with multiple inheritance. Now thankfully, if you have two interfaces that have a default implementation for a single method signature, and you try to have a class implement both interfaces, Java will refuse to compile that class. Many people complained about this ambiguity with allowing public static variables in interfaces, saying it was ambiguous if two interfaces had the same variable (hence languages like C# that disallow that sort of thing), but this is a bit harder to counter.

I see how this functionality can be useful, and I’m not going to make any definitive claims until I’ve seen it used in practice, but I must say it makes me nervous. That being said, let’s look a bit more into what this functionality might be used for instead of its intended purpose.

Possible Misuse

Earlier we talked about the problems of ambiguity introduced with multiple inheritance, but what about the things the compiler won’t catch? What’s to stop me from simply using interfaces like I would an abstract class (typically some abstract methods and a few implemented ones) but having the advantage of inheriting from more than one? This isn’t bad, per say, but it will encourage complex design that Java tried to avoid. There are few cases where you actually need multiple inheritance to solve a problem that you can’t do with the single inheritance style of building a framework of abstract classes.

Summary

In summary, while Java 8 does offer a lot of cool new things, some seem to go against the fundamentals of the early Java. Whether that is good or bad, only time will tell. If you have any differing opinions, please feel free to share them. Additionally, whenever you start using Java 8 on your project, feel free to share your success or horror stories. I can be reached at [email protected].

References

1 – http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

One thought to “Java 8 Default Methods and Multiple Inheritance”

  • pranit patil

    worth reading blog! for java beginner to know about these java 8 default methods and multiple inheritance, i have been looking for how inheritance works in default methods and now i have clear insights into this. Thanks a lot for sharing this blog.

    Reply

Leave a comment

Your email address will not be published. Required fields are marked *

X