SubclassOne
延伸ClassOne
并实现InterfaceOne
,两者都有 void doSomething(){}
方法。但是,编译器显示错误消息,
doSomething() in ClassOne can't implement doSomething() in InterfaceOne, attempt to assign weaker access privilege, was public
有人可以告诉我为什么编译器会显示这个特定的消息吗?其背后的原因是什么?
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) {
}
}
最佳答案
在这种情况下,您的SubclassOne
将提供一个同时满足父类(super class)和接口(interface)的实现。因此,由于两个方法的访问修饰符存在冲突,因此您会收到这样的错误消息。
(您的 ClassOne
的访问修饰符不是 public
。它是包可见的。)
关于java - ClassOne 中的 doSomething() 无法实现 InterfaceOne 中的 doSomething(),尝试分配较弱的访问权限,已公开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36527651/