我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码。接口中定义的方法必须在接口的非抽象子类中实现。下面就是关于接口的一个例子:

    1. public interface SimpleInterface {
    2. public void doSomeWork();
    3. }
    4. class SimpleInterfaceImpl implements SimpleInterface{
    5. @Override
    6. public void doSomeWork() {
    7. System.out.println( "Do Some Work implementation in the class" );
    8. }
    9. public static void main(String[] args) {
    10. SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    11. simpObj.doSomeWork();
    12. }
    13. }

    那么,如果我们在SimpleInterface里面添加一个新方法,会怎样呢?

    1. public interface SimpleInterface {
    2. public void doSomeWork();
    3. public void doSomeOtherWork();
    4. }

    如果我们尝试编译上面的这段代码,会得到如下结果:

    1. $javac .\SimpleInterface.java
    2. .\SimpleInterface.java:18: error: SimpleInterfaceImpl is not abstract and does not
    3. override abstract method doSomeOtherWork() in SimpleInterface
    4. class SimpleInterfaceImpl implements SimpleInterface{
    5. ^
    6. 1 error

    因为接口有这个语法限制,所以要直接改变/扩展接口内的方法变得非常困难。我们在尝试强化Java 8 Collections API,让其支持lambda表达式的时候,就面临了这样的挑战。为了克服这个困难,Java 8中引入了一个新的概念,叫做default方法,也可以称为Defender方法,或者虚拟扩展方法(Virtual extension methods)。
    Default方法是指,在接口内部包含了一些默认的方法实现(也就是接口中可以包含方法体,这打破了Java之前版本对接口的语法限制),从而使得接口在进行扩展的时候,不会破坏与接口相关的实现类代码。接下来,让我们看一个例子:

    1. public interface SimpleInterface {
    2. public void doSomeWork();
    3. //A default method in the interface created using "default" keyword
    4. //使用default关键字创在interface中直接创建一个default方法,该方法包含了具体的实现代码
    5. default public void doSomeOtherWork(){
    6. System.out.println( "DoSomeOtherWork implementation in the interface" );
    7. }
    8. }
    9. class SimpleInterfaceImpl implements SimpleInterface{
    10. @Override
    11. public void doSomeWork() {
    12. System.out.println( "Do Some Work implementation in the class" );
    13. }
    14. /*
    15. * Not required to override to provide an implementation
    16. * for doSomeOtherWork.
    17. * 在SimpleInterfaceImpl里,不需要再去实现接口中定义的doSomeOtherWork方法
    18. */
    19. public static void main( String [] args) {
    20. SimpleInterfaceImpl simpObj = new SimpleInterfaceImpl();
    21. simpObj.doSomeWork();
    22. simpObj.doSomeOtherWork();
    23. }
    24. }

    该程序的输出是:

    1. Do Some Work implementation in the class
    2. DoSomeOtherWork implementation in the interface

    以上是对default方法的一个非常简要的介绍。如果有兴趣的话,还可以通过看这篇文档,来获取更深层次的理解。
    更新:
    现在大家问得比较多的一个问题是:如果一个类实现了两个接口(可以看做是“多继承”),这两个接口又同时都包含了一个名字相同的default方法,那么会发生什么情况? 在这样的情况下,编译器会报错。让我用例子来解释一下:

    public interface InterfaceWithDefaultMethod {
       public void someMethod();
       default public void someOtherMethod(){
         System.out.println( "Default method implementation in the interface" );
       }
    }
    public interface InterfaceWithAnotherDefMethod {
       default public void someOtherMethod(){
         System.out.println( "Default method implementation in the interface" );
       }
    }
    

    然后我们定义一个类,同时实现以上两个接口:

    public class DefaultMethodSample implements
       InterfaceWithDefaultMethod, InterfaceWithAnotherDefMethod{
    
       @Override
       public void someMethod(){
         System.out.println( "Some method implementation in the class" );
       }
       public static void main(String[] args) {
         DefaultMethodSample def1 = new DefaultMethodSample();
         def1.someMethod();
         def1.someOtherMethod();
       } 
    }
    

    如果编译以上的代码,会得到一个编译器错误,如下所示。因为编译器不知道应该在两个同名的default方法中选择哪一个,因此产生了二义性。