I have the following problem:
I want to have an interface, which is public, but has one method that I want to be protected. In the example below, methodB() is the method that I want protected. If I declare it as protected, the compiler complains. So I leave it without any decorations, as shown below.
public interface MyInterface {
public int methodA();
void methodB();
}
Now, suppose I want to have a class that implements MyInterface. I try the following:
public class MyClass implements MyInterface {
public int methodA() {
return 42;
}
void methodB() {
System.out.println("This is my secret.");
}
}
The compiler complains again, saying that I am "attempting to assign weaker access privileges; was public". I seem to have no other choice than declare methodB in class MyClass as public.
My first question is: Please explain the logic here.
I want methodB to be declared in MyInterface, so that I may call within my own package:
o.methodB()
on any object o of type MyInterface, without having to cast it to a concrete class (that's the beauty of polymorphism).
My second question is: What shall I do?