类 Abstract 类和接口方法优先级

类中的实现(包括抽象声明)优先于所有接口默认值。

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