Help coderanch get a
new server
by contributing to the fundraiser
  • 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

class inside interface

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the purpose of defining an inner class inside an interface?
Thanks.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three examples from The Java Programming Language 5.6
1. To return multiple values from an interface�s method.

2. To hold shared data.

All classes that implement SharedData share a common state via the data reference.
3. To define a (partial or complete) default implementation for an interface.
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene. Class defined inside interface would be abstract?
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class declared within an interface is implicitly static so an instance of the nested class can be created without first making a futile attempt to create an instance of the enclosing interface.

Found this in Dans mock explanation.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are nested Interface classes a good programming practice or is there any other workaround to better address this kind of implementation?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paulo Freitas:
Are nested Interface classes a good programming practice or is there any other workaround to better address this kind of implementation?


Well, you can always work around this by putting the class outside the interface.
Is a class enclosed in an interface good programming practice? Well, this is probably getting close to a religious debate about what is good and what is not good practice.
However, my opinion is this:
If a class is so tightly coupled with an interface that the interface relies on that class in order to compile (such as returning an object of that class type) AND no other class uses that class outside of the methods defined in that interface - then, maybe it's okay.
In case you can't tell from that statement, I find classes enclosed within interfaces to be a bit confusing (not that they're hard to understand, you just don't see them often). So, I tend to shy away from them.
So, there's the "wishy washy" answer for the week. :roll: I think it can be acceptable in some cases, but I certainly wouldn't make a common practice of it.
Corey
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic