aspose file tools*
The moose likes Java in General and the fly likes Class within Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class within Interface" Watch "Class within Interface" New topic
Author

Class within Interface

David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Hello,
is there any reason or purpose to define a class within an interface?


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
zmarak ahmad djan
Greenhorn

Joined: Sep 02, 2004
Posts: 20


have fun

zmarak

I've never been a private but I actually *am* a publican


zmarak (publican)
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
I was taught that interfaces must have ALL abstract methods. But it looks from your post that if you have an abstract class in an interface, anything can be in that abstract class? (methods with bodies)?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Interfaces have all abstract methods implicitly except for those declared within nested classes.

Note that the given example is poor in that abstract classes with all concrete methods is generally poor.
The class needn't be abstract for it to be a member of an interface.
I use classes within interfaces to provide some implementation of the interface that is at the same level of abstraction as the interface - for example, a null implementation.


Tony Morris
Java Q&A (FAQ, Trivia)
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Hello Tony,
could you provide some example or detailed explanation of nested classes within interface? I'm not sure how to use nested class in interface.
Thanks
zmarak ahmad djan
Greenhorn

Joined: Sep 02, 2004
Posts: 20
Originally posted by Tony Morris:
The class needn't be abstract for it to be a member of an interface.
.


absolutely right

the class in the example does not need to be abstract

but in this example there was a pedagogical intent in doing so
I was trying to give some hints on why the class is here
and why it should be here.
designing a class that implements the interface gives some clues
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Hi Zmarak,
can you show the usage of your interface and what role play the inner class there.
Thanks

Something like
zmarak ahmad djan
Greenhorn

Joined: Sep 02, 2004
Posts: 20
two different points here :
-why the class in the interface ?
- usage ?

point one : notice that the class and the interface definition
depend on each other. if you declare this as two different compilation units
it would be possible but would need to be compiled together
the fact that the class is IN the interface expresses the fact
that the definition of the interfaces need that class.

point two:
- A) write a base class named "NormalProduct" and tax it.
all derived class will derive from this class.

-B) now a 1.5 feature
write a derived Interface IsTaxableSpecial extends IsTaxable
where the method getTaxAgent returns a local subclass of TaxAgent
write a class that derives from "NormalProduct" but implements
IsTaxableSpecial.
you're in for a (happy) surprise (courtesy J2SE 5)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class within Interface