jeudi 26 février 2015

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



For some reason, I can't declare a method in an Interface as package-only; it automatically declares as public. Here is the simplified code:



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...
}

}


When I try to compile this, these errors come, all connected:



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



Does anybody know how to fix this, or do I have to keep my Interfaces public to the whole Java code, not just open to the package? For security reasons, I want to keep the methods open only to the package, but it needed, I will try to find how to solve the problem another way.




Aucun commentaire:

Enregistrer un commentaire