Java 8 default methods
When used within an interface, default creates a method body that is substituted whenever the interface is implemented without defining that method.
// interfaces/AnInterface.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. interface AnInterface { void firstMethod(); void secondMethod(); // void newMethod(); // [1] // int i; //[2] you need to initialize i. // int j = 0; // fine, static & final // static final int k = 1;// fine // private int ii = 2; //interfaces/AnInterface.java:17: error: modifier private not allowed here // private int ii = 2; // ^ // 1 error }
// interfaces/AnImplementation.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. public class AnImplementation implements AnInterface { public void firstMethod() { System.out.println("firstMethod"); } public void secondMethod() { System.out.println("secondMethod"); } public static void main(String[] args) { AnInterface i = new AnImplementation(); i.firstMethod(); i.secondMethod(); } } /* Output: firstMethod secondMethod */
when [1] in AnInterface is uncommented, the compile error is:
interfaces/AnImplementation.java:6: error: AnImplementation is not abstract and
does not override abstract method newMethod() in AnInterface
public class AnImplementation implements AnInterface {
^
1 error
If we use the default
// interfaces/InterfaceWithDefault.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
interface InterfaceWithDefault {
void firstMethod();
void secondMethod();
default void newMethod() {
System.out.println("newMethod");
}
}
// interfaces/Implementation2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
public class Implementation2 implements InterfaceWithDefault {
public void firstMethod() {
System.out.println("firstMethod");
}
public void secondMethod() {
System.out.println("secondMethod");
}
public static void main(String[] args) {
InterfaceWithDefault i = new Implementation2();
i.firstMethod();
i.secondMethod();
i.newMethod();
}
}
/* Output:
firstMethod
secondMethod
newMethod
*/
The compelling reason to add default methods is that they allow you to add methods to an existing interface without breaking all the code that already uses that interface. default methods are sometimes also called defender methods or virtual extension methods .
referecnes:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/AnInterface.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/AnImplementation.java
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/InterfaceWithDefault.java
5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/interfaces/Implementation2.java