Java - 继承方法com.example.project.ConcreteA不能隐藏抽象方法com.example.project.MyInterface

Java - the inherited method com.example.project.ConcreteA cannot hide the abstract method in com.example.project.MyInterface

出于某种原因,我无法将接口中的方法声明为仅包;它自动声明为 public。这是简化的代码:

package com.example.project;

public interface MyInterface {

    void foo();
    Thing bar(); // The class Thing is in the com.example.project package, but what it does isn't important.

}



package com.example.project;

public abstract class SimpleConcrete implements MyInterface { // Initializes all methods as hooks
    
    protected Thing bar = new Thing();
    
    void foo() {}
    Thing bar() { return bar }
    
}
    


package com.example.project;

public class ConcreteA extends SimpleConcrete {
    
    void bar() {
        // code here...
    }
    
}



package com.example.project;

public class ConcreteB extends SimpleConcrete {
    
    void bar() {
        // more code here...
    }
    
}

当我尝试编译它时,出现了这些错误,全部连接:

File: C:\ProjectFolder\com\example\project\SimpleConcrete.java [line: 7]

Error: Cannot reduce the visibility of the inherited method from me.mathmaniac.everworlds.Block

File: C:\ProjectFolder\com\example\project\ConcreteA.java [line: 3]

Error: The inherited method com.example.project.SimpleConcrete.foo() cannot hide the public abstract method in com.example.project.MyInterface

File: C:\ProjectFolder\com\example\project\ConcreteB.java [line: 3]

Error: The inherited method com.example.project.SimpleConcrete.foo() cannot hide the public abstract method in com.example.project.MyInterface

有谁知道如何解决这个问题,还是我必须将我的接口 public 保留到整个 Java 代码,而不仅仅是打开包? 出于安全原因,我想只对包开放这些方法,但它需要,我会尝试找到其他方法来解决问题。

接口中声明的所有方法都是implicitly public

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

要对方法使用任何其他访问修饰符,您必须将接口转换为 abstract class,并将方法转换为 abstract 方法。

Java 中接口内的所有方法都是隐式的 public。有关详细信息,请参阅此处:http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html

你可以这样声明接口包级别:

interface MyInterface {

    void foo();
    Thing bar();
}

这至少会将其封装在该包中。