How to Use Default Methods with Java 8 Interfaces

  • google plus

In Brief...

Adding a new method to an existing interface traditionally has been a problem in Java because all implementations must be updated to include the new method or else a compile error will result. In Java 8, the new method can be declared as a default method in the interface and implementation logic can be provided in the interface itself. Therefore, existing implementations of the interface will continue to work successfully.

To understand how to use default methods in Java 8, follow these 15 steps.

Take our Introduction to Java Training course for free.

See the Course Outline and Register

Instructions

  1. First you will code the interface. In your text editor, type in the following Java statements:
    Interface With Static Method
    The interface defines one method for an integer add operation.
  2. Save your file as ArithmeticInterface.java.
  3. Open a command prompt and navigate to the directory containing your Java interface. Then type in the command to compile the source and hit Enter.
    Compile Source for Default Method Interface
  4. Now you will create the program that implements the interface and tests the implementation. Create a new file in your text editor. Type in the following Java statements:
    Java Source for Test Default Method Interface
    This file contains two classes. The first class, UseArithmeticInterface, contains the main method that will test the interface implementation. The main method instantiates the interface implementation at line 3 and then calls the add method in the implementation class. The second class is the implementation class, ArithmeticInterfaceImpl, and is defined at line 7. This class provides an implementation for the add method.
  5. Save your file as UseArithmeticInterface.java.
  6. Return to the command prompt. Then type in the command to compile the source and hit Enter.
    Compile Source for Test Default Method Interface
  7. Now type in the command to run your program and hit Enter.
    Run Test Default Method Interface
    The output displays the result of calling the add method as implemented by the implementation class.
  8. The next step is to add another method signature to the interface. Open ArightmeticInterface.java in your editor and add the subtract method signature after the add method:
    Interface With Default Method Second Method
    The new method defines an integer subtraction.
  9. Save your changes.
  10. Type in the command to compile the source and hit Enter.
    Compile Source for Default Method Interface
  11. Now you will attempt to compile the tester file that contains the implementation. The compile will fail because the implementation does not implement the subtract method: the source and hit Enter.
    Compile Source for Test Default Method Interface
  12. In order to solve the compile issue you could either implement the subtract method in the implementation class or you could modify the interface definition and mark the subtract method as a default method. You will do the latter. Open ArightmeticInterface.java in your editor and modify the subtract method by adding the default keyword and method logic:
    Interface With Default Method Second Method
  13. Save your changes.
  14. Now, compile the source with the modified subtract method and hit Enter.
    Compile Source for Default Method Interface
  15. You will once again attempt to compile the tester file that contains the implementation. The compile will succeed this time. Type in the command to compile the source and hit Enter.
    Compile Source for Test Default Method Interface
    The compile is successful because the subtract method now has a default implementation.

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss