Yes, you can declare a nested interface like that. One use would be to limit the visibiilty of such a type.
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Arthur Blair
Ranch Hand
Joined: Sep 20, 2005
Posts: 71
posted
0
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?
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.
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
Joined: Sep 20, 2005
Posts: 71
posted
0
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
Joined: Jan 29, 2006
Posts: 20
posted
0
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%)
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.