aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes class inside interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "class inside interface" Watch "class inside interface" New topic
Author

class inside interface

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
what is the purpose of defining an inner class inside an interface?
Thanks.


Sunita<br />SCJP 1.4
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Oct 03, 2002
Posts: 257
Thanks Marlene. Class defined inside interface would be abstract?
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
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

Joined: Feb 26, 2003
Posts: 17
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class inside interface
 
Similar Threads
Inner Interfaces vs Nested Interfaces
what is the need of writing an inner class inside an interface or abstract class
how to access class defined in an interface
Mock Test Que...
class in the interface