How to Use Java 8′s Default Methods

How to Use Java 8's Default MethodsBefore Java 8 your interfaces could contain method declarations, but no implementation code. One of the new features of Java 8 is the option to provide default implementations for methods right in your interface code itself.

 

Backward Compatibility

The main reason for adding this functionality was to allow the Java 8 team to add new methods to the Collections API (e.g. the forEach() method).  Without this, existing third-party libraries that already implement the Java collections API would not have worked with Java 8.  These vendors would have been forced to upgrade their code for Java 8.  Even worse, they would have to create and maintain separate versions if they wanted to continue supporting their non-Java 8 users.

Instead of breaking backward compatibility, the Java 8 team came up with a clever solution.  Allow developers to include a default implementation for their methods right in the interface itself — very similar to abstract classes.  For example, the Iterable class now declares a default implementation for the forEach method.

Default Methods in Action

Let’s understand how you can use default methods with an example. Assume that you have an interface called NamedPerson.

You also have two classes (Student and Contact) that implement this interface.

Now, suppose you wanted to add a method that returns the full name of the person long after the interface had been released and implemented by other developers in their own apps.  You would start by adding the new method as usual.

If you left the interface like this, both your Student and Contact classes would have to change.  Plus any developer implementing this interface would also have to change their class.

To avoid this in Java 8, you just need to add a default implementation to your fullName() method in the NamedPerson interface itself.

Testing Default Methods

You can test this with a simple program.

This gives the following output.

Of course, you can always override fullName() at any time.  For example, if you wanted to use the format “Last Name, First Name” in the Contact class.

Running the program now gives you the following output.

As you can see, the Student class is using the default implementation from NamedPerson while the Contact class is using its own custom implementation.

Multiple Inheritance Conflicts

It is possible for your classes to implement two interfaces that both supply default implementations of the same method. For example, suppose you have another interface named AddressBookEntry.

You want the Contact class to implement both the NamedPerson and AddressBookEntry interfaces.

However, your code would not compile.  The compiler would throw the following error since it can’t figure out which default implementation to use.

Resolving this error is quite simple. You just need to provide a custom implementation of fullName() in the Contact class:

This resolves the error as the compiler can now use the class’ own implementation of fullName() and doesn’t have to choose one of its defaults.
 

 

5 Responses to “How to Use Java 8′s Default Methods”

  1. Wish they used better wording on that error message.

    Why couldn’t this:
    ‘class Contact inherits unrelated defaults for fullName()…’

    Be better worded like this:
    ‘class Contact inherits conflicting defaults for fullName()…’

    Just one word but it makes a world of difference when trying to debug/troubleshoot.

    My 0.02,

    Sal.

  2. Very well explained. Looking forward for a series of Java 8 new features explained and maintained this way (with code snippets). Thanks

Trackbacks/Pingbacks

  1. Get Started With Lambda Expressions in Java 8 - Stack Hunter - May 28, 2014

    […] your interface has only one abstract method. If you need to have more methods, then use the “Default Methods” feature of Java […]