File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Nested Interfaces" Watch "Nested Interfaces" New topic
Author

Nested Interfaces

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
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?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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
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?
Hamid Virani
Ranch Hand

Joined: Jul 26, 2005
Posts: 41
You may have another nested class or an inner class which may implement that.
For E.g.
Valentin Jacquemin
Ranch Hand

Joined: Feb 15, 2006
Posts: 35
I don't see the advantage of it....

I don't speak English very well sorry...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17243
    
    1

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
santhosh sharma
Greenhorn

Joined: Jan 29, 2006
Posts: 20
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

Joined: Mar 17, 2000
Posts: 5782
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
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
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.
 
subject: Nested Interfaces
 
Similar Threads
top level nested interfaces
Implementing a nested toplevel interface
What is the motivation behind a nested interface?
Nested Interface?? in SCJP 1.4
naming of an interface