• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

access modifiers for Members in an interface

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
It would be nice if some one could answer this question regarding the accessor modifiers for members in an interface.
Rule: members declared inside the interface are implicitly public.
My analysis:
1- members can not be private as it would be useless as methods in interface are abstract.
2- members can not be protected as class implement interface but does not extend it.
But why can not they be "package visible" ?
or,
please explain the philosphy behind making the members in an interface always "public"
regards
Kapil
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what use would it be to allow interfaces to declare package-accessible methods? This would mean that the whole interface could only be used within its own package. Assuming there's actually a use for this, you can achieve the same effect by making the whole interface package-accessible. (I.e. declare it as "interface" rather than "public interface".) Note that the existing rules say that an interface's members are implicitly public - but that doesn't apply to the interface as a whole, which can be package or public, just like a class. (Nested classes or interfaces can also be private or protected; I'm just taking about top-level classes and interfaces.)
 
Kapil Sachdeva
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have to say regarding this:
package p1;
public interface myInterface{
int i= 1;
int j= 2;
}
Lets say that I do not want i to be visible outside the package p1.
regards & thanks
Kapil
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just remove the word "public", and now the interface cannot be used at all outside the current package. It's a little weird, because the methods now technically have public access while the interface does not. But the public access is meaningless here since no one outside the package can make any use of the interface. It's like saying "all the rooms in this buiding are unlocked, but not any of the outside doors." If you can't get in the building, it doesn't matter whether the inner doors are locked or not.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
"all the rooms in this buiding are unlocked, but not any of the outside doors." If you can't get in the building, it doesn't matter whether the inner doors are locked or not.


Actually it is more like all the rooms in this buiding are unlocked, "but not any of the outside doors or you can come thru the ventalitors" if a public inteface extends the package level interface, then the former will contain the methods define in the latter. :roll:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic