I know that we can create an inner class inside an interface but i want to know that why we'll create an inner class inside an interface.
I mean what is the use of creating inner class inside an interface and what is the advantage of it.
Let me try to help you out with an indirect answer for I don't have a direct answer.
In my knowledge, there are two kinds of Inner classes(in a general term): Nested and Local. The nested class is one that is enclosed under a top level type. The local class is one which is declared inside a method or a block; furthermore, the local class is of two kinds: named local class and anonymous local class. All of these can be declared both as a static or as an instance member.
That being stated, practically, I have encountered two examples of inner classes as a recurring pattern: the one as a private static class, and the other as a annonymous inner class. A private static class is where a functionaly needs to be encapsulated in a single class that is used only be a top level class -- the converse is true as well. The idea here is that members of declared nested class are only accessible(or accessed) inside the enclosed class(type) -- not by any other class. With regards to anonymous inner class: event handlers and mock objects are a few examples.
The reason lies in encapsulation and high cohesion concept of Object Oriented Design.
because an inner class is associated with an instance, it cannot define any static members itself.
You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers — private, public, and protected — to restrict access to inner classes, just as you do to other class members.
An inner class is by definition not static. Any class or interface declared inside an interface is implicitly static, and therefore not an inner class. This may seem pedantic, but it's helpful for understanding how the JLS describes this stuff.
Randall: note that neither of those points apply inside an interface, since any class or interface declared inside an interface will be implicitly public and static, and thus not associated with any instance, and not private or protected.
Like Winston, I can't think of an existing API example of a nested class inside an interface. However I have always felt that it would be an appropriate way to declare custom exceptions specific to an interface. Haven't seen anyone do it yet though.
Mike Simmons wrote:An inner class is by definition not static. Any class or interface declared inside an interface is implicitly static, and therefore not an inner class. This may seem pedantic, but it's helpful for understanding how the JLS describes this stuff.
Not at all, and you're quite right. Fact is, I can't remember more than a few inner classes I've ever created.
Like Winston, I can't think of an existing API example of a nested class inside an interface. However I have always felt that it would be an appropriate way to declare custom exceptions specific to an interface.
Good point. I'll have to remember that for future reference
No need to apologize. We used to discourage this before but things changed and I guess we're cool with it now.
Seems like a convenience more so than anything else.
It could also be a way to manage namespaces and access. It says "This is designed to be used only in the same context as the enclosing interface. These logically go together. Using it in any other context doesn't make a lot of sense."