一、公有静态常量的冲突
如果多个接口中有相同的常量,那么实现类就无法继承,会编译报错
interface A{
int NUM1 = 10;
}
interface B{
int NUM2 = 20;
int NUM1 = 30;
}
class Impl implements A,B{
}
public class Tests {
/*
* 公有静态常量的冲突:如果多个接口中有相同的常量,那么实现类就无法继承
* */
public static void main (String[] args) {
//System.out.println(Impl.NUM1); //编译报错
System.out.println(Impl.NUM2);
}
}
二、公有抽象方法的冲突
如果两个接口中的抽象方法相同,实现类只需要重写一个
interface A{
void method();
}
interface B{
void method();
}
class Impl implements A,B{
@Override
public void method () {
System.out.println("实现类方法重写");
}
}
public class Test {
}
三、公有默认方法冲突
实现类必须重写一次默认方法(default),一个类可以实现多个接口,但是如果实现的多个接口有重名的默认值就必须通过在是实现类中重写默认方法。
interface A{
public default void method(){
System.out.println("A接口的默认方法method");
}
}
interface B{
public default void method(){
System.out.println("B接口的默认方法method");
}
}
class Impl implements A,B{
@Override
public void method () {
System.out.println("实现类重写的默认方法");
}
}
public class Test {
public static void main (String[] args) {
Impl impl = new Impl();
impl.method();
}
}
四、公有静态方法冲突
静态方法是直接属于接口的,不能被继承,所以不存在冲突