Home » Java » Core Java » Introduction to Default Methods (Defender Methods) in Java 8

About Mohamed Sanaulla

Mohamed Sanaulla

Introduction to Default Methods (Defender Methods) in Java 8

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.
 

Reference: Introduction to Default Methods (Defender Methods) in Java 8 from our JCG partner Mohamed Sanaulla at the Experiences Unlimited blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

5 Comments on "Introduction to Default Methods (Defender Methods) in Java 8"

Notify of
avatar
Sort by:   newest | oldest | most voted
ramesh
Guest

this concept we don’t need if we follow ” code for interface and adopter design pattern”

Mohamed Sanaulla
Guest

This concept was introduced out of need. And the need was to enhance the interface with new methods without breaking the implementing classes.

fayss
Guest

and what appends if your class implements two interfaces that declare the same default methode name ? big trouble ?

public interface SimpleInterface {
public void doSomeWork();
default public void doSomeOtherWork(){
System.out.println(‘DoSomeOtherWork implementation in the interface’);
}
}

public interface SecondSimpleInterface {
public void doSomeWork();
default public void doSomeOtherWork(){
System.out.println(‘DoSomeStuffDifferent implementation in the interface’);
}
}

class SimpleInterfaceImpl implements SimpleInterface,SecondSimpleInterface { …..}

Mohamed Sanaulla
Guest

If you happen to read the Defender method document I have linked in the post above- the compiler would flag this as an error. I will update the post with this scenario.

trickmicro
Guest

Actually, if we are able to provide body for a method in an interface means violating the meaning of an interface. An interface here is turned into an abstract class in one way.

wpDiscuz