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 keyword and provide a default definition for newMethod() , all existing uses of the interface can continue to work(before Java 8 this was not work).
// 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
本文深入探讨了Java接口中默认方法的使用,解释了如何通过default关键字为接口方法提供实现,使得现有接口可以在不破坏兼容性的情况下添加新方法。通过示例代码展示了默认方法的工作原理及其在实际编程中的应用。
691

被折叠的 条评论
为什么被折叠?



