• 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 within Interface

 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
is there any reason or purpose to define a class within an interface?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


have fun

zmarak

I've never been a private but I actually *am* a publican
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic