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

doSomething() in ClassOne can't implement doSomething() in InterfaceOne, attempt to assign weaker access privilege, was public

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) {

    }
}

接口中的方法是public。没有访问修饰符 package-private 的方法,即较弱的访问权限。在 ClassOne

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

您可以在 documentation

处看到访问修饰符 table

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

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