• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface Access  RSS feed

 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am seeking clarification on following:
1. An interface can be defined as public or default (package).
2. When an interface is defined as public, it can be implemented by any class. When an interface is defined as default (package), it can only be implemented by class in same package.
3. Interface method declarations are always public.
4. Implementing methods (in implementing class) must also be public.

If methods are always public, why the ability for interface declaration to be default (package)?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if a method is public, if the class or interface is not public, then something outside the package cannot access the method, since the thing outside the package cannot access the class or interface.
 
Craig Oliver
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dirk

Are you able to confirm if my four statements are correct?

Thanks
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are correct. Note that it's possible to code a method declaration in an interface without explicitly stating the accessibility of the method, where it might appear to be "default" or "package" accessible. In fact, "public" accessibility is implied, since all methods declared in an interface are public. So, the compiler effectively fills in "public" where the programmer might not have explicitly stated the accessibility.

Clear?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!