aspose file tools*
The moose likes Beginning Java and the fly likes Reason to create inner class in an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reason to create inner class in an interface" Watch "Reason to create inner class in an interface" New topic
Author

Reason to create inner class in an interface

P Malhotra
Greenhorn

Joined: Jul 27, 2011
Posts: 7

Hi everyone,




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.
Rod Singh
Greenhorn

Joined: Feb 10, 2012
Posts: 16
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Java Tutorials
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

a good tutorial. two things i picked up from it.
because an inner class is associated with an instance, it cannot define any static members itself.

and
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.


SCJP
Visit my download page
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

P Malhotra wrote:I mean what is the use of creating inner class inside an interface and what is the advantage of it.

An inner class inside an interface is quite rare; and off the top of my head I can't think of an example.

An example of a nested interface is Map.Entry, and I have put enums inside an interface before now.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

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

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reason to create inner class in an interface