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.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

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

  1. ramesh says:

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

  2. fayss says:

    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 { …..}

  3. trickmicro says:

    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.

Leave a Reply


+ three = 12



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books