File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packages " Watch "Packages " New topic


Daslan Govender

Joined: Jan 14, 2005
Posts: 19
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.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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.
Divya Mehrotra

Joined: Mar 01, 2005
Posts: 21
If you do not specify public modifier, your interface will be accessible only to classes that are defined in the same package as the interface.

please refer the following link :-

Divya Mehrotra
Daslan Govender

Joined: Jan 14, 2005
Posts: 19
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Packages
It's not a secret anymore!