如果我在接口和抽象 class 中使用相同的名称方法,我无法理解 superclass、接口和子 class 之间的关系

I can't understand relationship between superclass, interface and child class if I use the same name methods in both interface and abstract class

public interface Pet { // Pet Interface
    default void introduce() {
        System.out.println("Interface");
    } 
}

abstract public class Animal {
    public void introduce() {
        System.out.println("Animal class");
    }
}

public class Dog extends Animal implements Pet{}

现在,当我在 Dog 上调用 introduce 方法时,实际发生了什么?

默认方法总是'last in line'。如果任何方法在 classes 链的层次结构中的任何位置(因此,这里 'Dog' 在顶部,然后是 Animal,然后是 Object - 这些是层次结构中涉及的 classes ) 比赛,总是赢。

仅当在整个 class 链中找到零个方法,并且接口链恰好包含 1 个具有默认实现的方法时,才会选择该方法。如果有多个,那么你的代码将无法编译(如果你有 class A implements B, C {} 并且 BC 都定义了相同的方法并且都有默认的实现,那么你如果不将该方法显式写入 A,则无法编译 A。