Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reason to create inner class in an interface

 
P Malhotra
Greenhorn
Posts: 7
MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50258
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorials
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to reply to an old thread, but I was looking into this myself and I came across interface methods that use inner classes as their thrown exceptions e.g.



Seems like a convenience more so than anything else.
 
Junilu Lacar
Bartender
Pie
Posts: 7778
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Smith wrote:Sorry to reply to an old thread

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."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic