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

What is the motivation behind a nested interface?

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a prcatical example of a nested interface and when can it be used.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One reason A nested interface is quite usefull is when you have to declare a set of constant variables so you can declare final variables that are implicitly static and refer to it next.And you don't want to write it on another file.
For Example:

...Maybe there are other more important motivation!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For another use of a nested interface, check out java.util.Map.Entry. The Map interface needed to make use of another interface, Entry. The Entry interface is really only used in connection with a Map - so while it could have been defined in a separate file, it's a bit cleaner to define it within the Map interface. This is one of the primary uses of static member classes - defining a class that, while it could have been a top-level class in a separate file, is so closely tied with another class that you'd really rather define it in the same file. It only makes sense to allow this for interfaces as well as classes.
Map.Entry is an interface nested in an interface; it's also possible to have an interface nested in a class, or a class nested in an interface - though I don't know of an example of these offhand. All these are necessarily static member classes or interfaces - any class or interface nested in an interface is implicitly static, and any interface nested in a class is also implicitly static. So even if you don't declare them as static, they are.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know if useful at all, but a class nested within an interface allows the interface to provide some code to the classes implementing it.

[ February 09, 2003: Message edited by: Jose Botella ]
[ February 09, 2003: Message edited by: Jose Botella ]
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was redirected from another thread to this one. I have found this amazing link.

http://littletutorials.com/2008/03/06/static-nested-interfaces/

HTH.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the reason I can think of having nested classes is to encapsulate related structure inside the main class, just like Entry object in case of HashMap . Inner classes have separate class file than main class and add into size if you are too much concern about size.
reply
    Bookmark Topic Watch Topic
  • New Topic