• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Private / Protected Interface

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can someone show me how a private / protected interface is coded?
I got the below from JLS but am still in doubt.
The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration (�8.5) and are discussed in �8.5.1.
Thanks in advance.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What this means is that you can't make top-level interfaces private or protected:
This won't compile:

But within a class, they're ok:

External classes wishing to use the interfaces defined in Enclosing are bound by the access modifiers for each. For example, no methods outside of class Enclosing can use interface Foo, since it's private.
But, a subclass of Bar, or a class in the same package as Bar, could use this interface by qualifying it with it's Enclosing class:
public class Outside implements Enclosing.Bar {}
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob. But is there any use of defining interfaces in a class?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure there is! It really depends on the problem you're trying to solve.
One of the keys of OO design is encapuslation. That pertains to organizing your types and namespace as well as data. If a class or set of classes need a certain definition, like an interface, but the interface isn't generally useful outside that class, or you want the class to tightly control use of the interface, then defining the interface inside the "main" class that uses it may be a good design decision.
It really depends on the nature of your particular design problem. It's good to understand how this mechanism works so you can take advantage of it when the need arises.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
according to the JSL
 
Axl Rose
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
according to the JSL 8.1.2
interfaces are never inner.
can you explain
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nested interfaces are implicitly static; unlike a nested class, which can be static or not, interfaces can ONLY be static when declared inside another class/interface.
This makes sense, since an interface only describes an API, it provides no implementation. Since you would never be able to instantiate an interface, it would make no sense to allow an "inner" interface.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Axl,
An interface which is defined within a class is called a "Nested, top-level interface." The only time we use the word "inner" is when we're talking about non-static classes defined within another class - those are called "inner classes."
Hope that helps,
Corey
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
You mentioned that "Nested interfaces are implicitly static; unlike a nested class, which can be static or not, interfaces can ONLY be static when declared inside another class/interface."
As what I know, interfaces are implicitly abstract. How can the interface be static when it's abstract? Abstract and static don't go together, am I right?
Choi Nang
 
Shiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic