• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Inner Classes

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we write inner class inside an interface? If yes then what does it means?
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can. What does it mean? I don't really know. What does it mean to declare an inner class inside a class? Whatever it means there, it means the same thing in an interface.
 
nishant kumarmca
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for replying but how can we instantiate the inner class which is inside the interface? And one more can we also write Method-Local Inner Classes inside the interface?
 
Paul Clapham
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would create an instance of the interface's inner class in exactly the same way you would create an instance of a class's inner class. There is no difference in the syntax.

As for your question about method-local inner classes in an interface, the answer to that should be quite clear if you know what a method declaration looks like in an interface. No?
 
nishant kumarmca
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please explain me in which scenario we should use inner classes inside the interface?
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for bringing up this interesting question.

nishant kumarmca wrote:can you please explain me in which scenario we should use inner classes inside the interface?

After trying it in my system I found that, there is no special in 'declaring inner class inside an interface', because it is same as usual inner classes (classes inside another class)
 
Rancher
Posts: 5086
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Except when it isn't.

An important difference is that any class (or interface) declared inside an interface is implicitly static. This means it is not an inner class, by Java's definition of the term - because an inner class is not static. It is a static nested class, or static member class. Key differences are:

Static member class:
- has no reference to any enclosing class instance, because there isn't one
- can declare new static members

Inner (non-static) member class:
- has a reference to the enclosing class instance, if there is one
- cannot declare new static members (though it can inherit them)
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The primary goal of interface is to declare contract between implementation class and its user. It is ok to put constants there even in the form of inner classes but placing your logic to be just inappropriate. If you need namespace use subpackages
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A slightly different slant:





Classes that implement this interface have direct access to UTIL - thus


 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shanky : Thank you for a clear and useful example.

Jim ... ...
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic