一、公有静态常量的冲突
接口A和接口B中的常量相同,调用的话就会报错
interface A{
public static final int NUM1 = 10;
}
interface B{
public static final int NUM1 = 10;
public static final int NUM2 = 20;
}
interface C extends A,B{}
public class Test {
public static void main (String[] args) {
//System.out.println(C.NUM1);// 接口A和接口B中的常量相同给,调用的话就会报错
System.out.println(C.NUM2);
}
}
二、公有抽象方法的冲突
子类只会继承一个有冲突的抽象方法
interface A{
public abstract void method();
}
interface B{
public abstract void method();
}
interface C extends A,B{}
class Impl implements C{
@Override
public void method () {
System.out.println("实现接口的抽象方法");
}
}
public class Test {
public static void main (String[] args) {
Impl impl = new Impl();
impl.method();
}
}
三、公有默认方法的冲突
子接口中必须重写一次有冲突的默认方法
子接口重写父接口中的默认方法,必须加上default
interface A{
public default void method(){
System.out.println("A接口中的默认方法method");
}
}
interface B{
public default void method(){
System.out.println("B接口中的默认方法method");
}
}
interface C extends A,B{
@Override
default void method () {
System.out.println("重写父类接口中的method方法");
}
}
class Impl implements C{
}
public class Test {
public static void main (String[] args) {
Impl impl = new Impl();
impl.method();
}
}