This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Is it correct to say that when defining an interface, the only access modifier that a method can have is public? In fact the method signiture can be specfied without being prefixed by public.
If this is true, why when I define an interface in a package and I try to implement the interface outside the package, I get a compile error saying that the interface is not public in the package? It works perfectly if the method signiture in the interface is prefixed by public.
if you're writing an interface, and you're declaring some part of it as being something other than public, and the javac compiler is not telling you there's an error on that particular line... then you can, clearly, declare things to be other than public in an interface. however, doing so may or may not make any sense, and the error you're seeing might be happening because the remainder of your code (viewed as a whole) turns out not to make sense.
Yes, a method in an interface can ONLY be public. However, there is a difference between the visibility of the methods and the visibility of the interface itself and the methods in the interface. If the interface itself is not declared public, it doesn't generally matter what visibility the methods have.