• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reason to create inner class in an interface

 
Greenhorn
Posts: 7
MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Tutorials
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
reply
    Bookmark Topic Watch Topic
  • New Topic