We all know that interfaces in Java contain only method declarations and no implementations and any non-abstract class implementing the interface had to provide the implementation. Lets look at an example:

public interface SimpleInterface {
  public void doSomeWork();
}

class SimpleInterfaceImpl implements SimpleInterface{
  @Override
  public void doSomeWork() {
    System.out.println("Do Some Work implementation in the class");
  }

  public static void main(String[] args) {
    SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    simpObj.doSomeWork();
  }
}

Now what if I add a new method in the SimpleInterface?

public interface SimpleInterface {
  public void doSomeWork();
  public void doSomeOtherWork();
}

and if we try to compile the code we end up with:

$javac .\SimpleInterface.java
.\SimpleInterface.java:18: error: SimpleInterfaceImpl is not abstract and does not 
override abstract method doSomeOtherWork() in SimpleInterface
class SimpleInterfaceImpl implements SimpleInterface{
^
1 error

And this limitation makes it almost impossible to extend/improve the existing interfaces and APIs. The same challenge was faced while enhancing the Collections API in Java 8 to support lambda expressions in the API. To overcome this limitation a new concept is introduced in Java 8 called default methods which is also referred to as Defender Methods or Virtual extension methods.

Default methods are those methods which have some default implementation and helps in evolving the interfaces without breaking the existing code. Lets look at an example:

public interface SimpleInterface {
  public void doSomeWork();
  
  //A default method in the interface created using "default" keyword
  default public void doSomeOtherWork(){
    System.out.println("DoSomeOtherWork implementation in the interface");
  }
}

class SimpleInterfaceImpl implements SimpleInterface{
  @Override
  public void doSomeWork() {
    System.out.println("Do Some Work implementation in the class");
  }
  /*
   * Not required to override to provide an implementation 
   * for doSomeOtherWork.
   */

  public static void main(String[] args) {
    SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    simpObj.doSomeWork();
    simpObj.doSomeOtherWork();
  }
}

and the output is:

Do Some Work implementation in the class
DoSomeOtherWork implementation in the interface

This is a very brief introduction to default methods. One can read in depth about default methods here.

Update:
There was a concern about a class implementing 2 interfaces with each interface declaring a default method with the same name. In such cases the compiler would flag it as an error. Let me explain it with an example:

public interface InterfaceWithDefaultMethod {
  public void someMethod();
  default public void someOtherMethod(){
    System.out.println("Default method implementation in the interface");
  }
}
public interface InterfaceWithAnotherDefMethod {
  default public void someOtherMethod(){
    System.out.println("Default method implementation in the interface");
  }
}

And then a class implementing both the above interfaces.

public class DefaultMethodSample implements 
  InterfaceWithDefaultMethod, InterfaceWithAnotherDefMethod{
  
  @Override
  public void someMethod(){
    System.out.println("Some method implementation in the class");
  }
  public static void main(String[] args) {
    DefaultMethodSample def1 = new DefaultMethodSample();
    def1.someMethod();
    def1.someOtherMethod();
  }  
}

but this results in a compiler error as shown in the screenshot below and this way it avoids the confusion of which default method implementation to invoke.
Default Method Error

Tagged with:
 
About The Author

Mohamed Sanaulla

10 Responses to Introduction to Default Methods (Defender Methods) in Java 8

  1. Valera says:

    this is nice!

  2. Hi Mohamed, so with this change, how interfaces are different than abstract classes?

    • The difference between interfaces and abstract classes still are the same as they were in pre Java 8. Just that now interfaces can also contain implementations. And a class can implement multiple interfaces.

  3. Milan says:

    Hi Mohamend,
    the rule for interfaces tells us that there is no method implementation allowed inside of interface declaration. It means that the rule is broken.

    • You are right. With the new Java 8 the interfaces are allowed to provide default implementation for the method. But the Java team recommend to provide method implementation in an other class and then invoke that method from the interface. That way the implementation will be in another class but the interface will just have method invocation.

  4. [...] fixes this problem by allowing interface methods to have default implementations (sometimes called “defenders”), which can be overridden by derived classes but [...]

  5. yes, let’s add more garbage to an already bloated language!
    Interfaces must never contain any implementations !!!!

    Slowly, the Java language becomes a monster.
    sad.

    • Ike Love says:

      Thanks for your *great* insight there, very *helpful*! Putting default implementations on interfaces allows Java to support multiple inheritance. Google what it brings to the table, instead of trolling useful posts.

  6. LoicAG says:

    Nit-picking here… When you do:
    SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    doesn’t that defeat the purpose of having an interface? I’m not a Java expert, but I would have declared the object as such:
    SimpleInterface simpObj = new SimpleInterfaceImpl();
    although I guess it depends on what you intend to do with that object afterwards?

  7. saurav says:

    Whoa these days lots of buzz is going on default methods , once java 8 is officially release it i am sure that most of the applications will definitely going to use it.

    The reason why i think till now there was always a constraint that if we want to introduce some new method (prior to java 8) in an interface ,then it can break the whole application as all the implementation will be forced to provide an implementation for that ,so in that case we try to solve this problem by introducing a new interface to include that new method and i think here we violate the Single Responsibility Model (as its not stated anywhere).

    Again I personally feel that Abstract classes have their own cost of using them in terms of multiple inheritance and I also feel that Default methods and abstract classes have there own place in Java and they can’t replace each other.

    One more thing I found an interesting interesting article on Default methods here, it is little bit on the sarcastic side but a worth reading.
    http://lotusmediacentre.com/default-methods-in-java-8-explained/

Leave a Reply