ClassOne中的doSomething()无法在InterfaceOne中实现doSomething(),尝试分配较弱的访问权限,是公共的

时间:2016-04-10 08:42:05

标签: java interface subclass superclass

SubclassOne扩展ClassOne并实施InterfaceOne,两者都有void doSomething(){}方法。但是,编译器显示错误消息

  ClassOne中的

doSomething()无法实现doSomething()   InterfaceOne,尝试分配较弱的访问权限,是公开的

有人可以告诉我为什么编译器会显示此特定消息吗?它背后的原因是什么?

public class ClassOne {
    void doSomething(){
        System.out.println("do something from InterfaceMethod class");
    }
}


public interface InterfaceOne {
    default void doSomething(){
        System.out.println("do something from InterfaceOne");
    }
}


public class SubclassOne extends ClassOne implements InterfaceOne{
    public static void main(String[] args) {

    }
}

2 个答案:

答案 0 :(得分:2)

在这种情况下,SubclassOne将提供满足超类和接口的实现。因此,由于与两种方法的访问修饰符存在冲突,您将收到此类错误消息。

(您的ClassOne访问修饰符不是public。它的包可见。)

答案 1 :(得分:0)

接口中的方法是public。没有访问修饰符package-private的方法,即较弱的访问权限。将public修饰符添加到doSomething

中的ClassOne
public class ClassOne {
    public void doSomething(){
        System.out.println("do something from InterfaceMethod class");
    }
}

您可以在documentation

处查看访问修饰符表