Java error "modifier private not allowed here" can occur for any of the following reasons:
1. An outer class declared as private like below:
2. An interface method declared as private.
An interface can contain only public or package private method.
The correct code of the above is:
1. An outer class declared as private like below:
private class OuterClassAn outer class only be public or package private not private or protected. So you can write an outer class as below:
{
//...
}
public class OuterClassor
{
//...
}
class OuterClassThis is true for an interface also. An interface can only be public or package not private or protected.
{
//...
}
2. An interface method declared as private.
public interface TestInterface
{
private void method(); // wrong as the interface method is private
}
An interface can contain only public or package private method.
The correct code of the above is:
public interface TestInterfaceor
{
public void method();
}
public interface TestInterface
{
void method(); // interface method declared as package private
}
An interface can contain only public methods (or private static final fields). Since the interface specifies a set of exposed behaviors, all methods are implicitly public abstract. It really doesn't add a lot of value to explicitly specify these modifiers.
ReplyDeleteInterface fields(member variables) are public static final
ReplyDeleteI have interface with package private method, when I am implementing it in a class its giving me
ReplyDelete"attempting to assign weaker access privileges; was public" compiler error
I am using JDK 1.6
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
Please have a look at http://java-error-messages.blogspot.com/2011/05/attempting-to-assign-weaker-access.html
ReplyDelete