類 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