• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class within Interface

 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
is there any reason or purpose to define a class within an interface?
 
zmarak ahmad djan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


have fun

zmarak

I've never been a private but I actually *am* a publican
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic