• 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

Nested Interfaces

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to declare an interface like this:



...but why would you want to have a nested interface and what are it's implications?

Is it only available to nested classes?
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can declare a nested interface like that. One use would be to limit the visibiilty of such a type.
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Perhaps I didn't make myself clear in my post. I understand that is it possible to declare an interface in this way. I'm interested in what the implications are of creating such an interface.

If I have a class in the same package, it won't be able implement the inner interface. So can I assume that this inner interface is only available to classes that are members of the enclosing class?

...and why would you need to do this? Can someone provide a real world example?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have another nested class or an inner class which may implement that.
For E.g.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see the advantage of it....

I don't speak English very well sorry...
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, if it needed to have an inner interface because of the number of inner class that needed to implement, I would run away as fast as I can, because I feel it is going overboard, and would rather it be a more maintainable file, not 100,000 lines in one file (exaggeration there).

But you could create a whole inner factory with many objects implementing the same inner interface, then the code that uses the inner factory can code to an interface, in theory.

Mark
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi arthur,
I dont think nested interfaces can be declared as private
Nested interfaces are implicitly abstract and public

santhosh sharma (scjp 1.4 100%)
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, unlike top level interfaces that are implicitly public, nested interfaces can be declared private. Here's what JLS says


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.

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Santhosh,

Thats not correct. A top-level interface is implicitly abstract, but can be default or public. If it is public it needs to be in a file with the same name as the interface plus the extension .java

However, If an interface is nested in another class, it's access modifier can be private, default, protected or public. While if an interface is nested within another interface, it can be public or default.

...which brings me to my next question. Why on earth would you would to have an interface nested within another interface?

Arthur
 
santhosh sharma
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi arthur ,
I agree with you , there is no point in allowing an interface to be declared inside another interface , but hey

please check ,

interface a
{
private interface b{}
}

is giving me a compile time error
[illegal combination of access modifiers public and private]

santhosh sharma (scjp 1.4 100%)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic