1.两个接口中都定义了同名默认方法
public interface Named {
default String getName() {
return "named.";
}
}
public interface PersonInterface {
default String getName() {
return "person.";
}
}
会抛出编译错误。
覆盖方法即可:
public class Teacher implements PersonInterface, Named {
@Override
public String getName() {
return null;
}
}
2.父类与接口有同名方法
不会冲突,类优先原则
public class Teacher extends Person implements Named {
public static void main(String[] args) {
System.out.println(new Teacher().getName());
}
}