• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class inside interface

 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the purpose of defining an inner class inside an interface?
Thanks.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Paulo Freitas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic