Java 8 default methods

本文深入探讨了Java接口中默认方法的使用,解释了如何通过default关键字为接口方法提供实现,使得现有接口可以在不破坏兼容性的情况下添加新方法。通过示例代码展示了默认方法的工作原理及其在实际编程中的应用。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值