Java类,抽象类和接口方法的优先级
示例
类中的实现(包括抽象声明)优先于所有接口默认值。
抽象类方法优先于接口默认方法。
public interface Swim { default void backStroke() { System.out.println("Swim.backStroke"); } } public abstract class AbstractSwimmer implements Swim { public void backStroke() { System.out.println("AbstractSwimmer.backStroke"); } } public class FooSwimmer extends AbstractSwimmer { }
以下声明
new FooSwimmer().backStroke();
会产生
AbstractSwimmer.backStroke
类方法优先于接口默认方法
public interface Swim { default void backStroke() { System.out.println("Swim.backStroke"); } } public abstract class AbstractSwimmer implements Swim { } public class FooSwimmer extends AbstractSwimmer { public void backStroke() { System.out.println("FooSwimmer.backStroke"); } }
以下声明
new FooSwimmer().backStroke();
会产生
FooSwimmer.backStroke