Java 8 Default MethodsDefault methods for interfaces

Why should you care?

Programming to an interface. It's something we've been told is the right thing to do because it promotes flexible and extensible programs.

But here's one big issue with that approach. Before Java 8, you had to be extra sure that your interface design is “complete” if you release it into the wild.

Here's what I mean by that.

Let's assume you've published some code that other people will be using. This is typically done via releasing your app as a 3rd party plugin / library.

Within this library that you've released, you include many interfaces (because you're a good programmer and you've been programming to interfaces).

Let's say one of these interfaces is called MyWidgetService

public interface MyWidgetService {
  void doSomethingAwesome();
  void doAnotherAwesomeThing();
}

Done and done.

You're satisfied with this interface and the functionality it represents.

Until some new feature requests come in… And suddenly this interface is now out of date and needs one more abstract method: doTheAwesomestThing().

What's the issue right? Just add it in there and be done with it!

Not so fast. If you add another method in this interface, every other developer's application who relies on your app has the potential to break.

Wrath hath no fury like coders scorned.

Introducing Default Methods

Default methods allow you to add some actual functionality to your methods within the body of the method!

Whaaaaat?!

I know right.

So this means that you will be able to modify your existing interface by adding a new “default method” to it, and it won't cause a programmer uprising when they update their code base.

So how are these magical default methods created?

public interface MyWidgetService {
  void doSomethingAwesome();
  void doAnotherAwesomeThing();
  default void doTheAwesomestThing() {
    System.out.println("I'm the body of the 'doTheAwesomestThing()' method");
  }
}

Okay, so with our interface updated with our new default method, all implementing classes of this interface will inherit the default functionality of the doTheAwesomestThing() method (without any compilation errors).

Note: if you're not actually implementing any sort of useful code inside of your default method, then I would imagine that it would be a good practice to throw an exception in the body of the method letting programmers know that the method hasn't actually been implemented with useful code.

Default Method Inheritance

If a parent interface declares a default method, and a child interface implements the parent interface, then the child interface will also inherit the functionality of its parent's default method.

You can also override the functionality of the parent's default method in the child interface if you like.

And finally, of course, if you override the functionality of an interface's default method in a concrete class, then the implementation in the concrete class will take precedence when run.

Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.