• 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

Anonymous class cant do both ...

 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An anonymous class may implement an interface or extend a superclass,
but may not be declared to do both.
can anyone give an Example ??
Please
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sunil,
Here's an example that an anonymous inner class can extend a base class and at the same time implement an interface.

-Peter
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ClassInterfaceTest in the example given is not anonymous, since it has a name.
The creation of an anonymous class depends on an implied extension or implementation. The implication is completely taken from syntax, and the syntax itself does not allow for...uh..."multiple implications," if you will.
Here's one that gets used in GUI code all the time:

The subclass of WindowAdapter is understood from the context of the open brace following the constructor call. There's no name and no differentiation between a subclass and an implementation. Consider the implementation of ActionListener below:

So subclasses and implementations are syntactically identical when writing an anonymous class. That being said, there's simply no provision for doing this more than once per instance.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 09, 2001).]
 
sunil choudhary
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Michael,
NO wonder you wrote the most quoted guide on Java Certification.
Hats off to that.
Yes,I *too* have your book.
Sunil Choudhary
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael,
I haven't had my coffee yet (my excuse), so I don't quite see the distinction you are making between ClassInterfaceTest and your example.
From my example, I don't extend ClassInterfaceTest to create a subclass. In fact, ClassInterfaceTest is an abstract class. Furthermore, in the main() method I think I do create an anonymous class when I invoke the test() method.

Why isn't this a valid example of an anonymous class?
Thanks,
-Peter

[This message has been edited by Peter Tran (edited January 12, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it has a name. It is ClassInterfaceTest. The word anonymous means "not named". In your first example you showed an abstract class that extends another class and implements an interface.
Abstract is not the same an anonymous.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy,
I see what you're saying, but in Michael's two example,

Both, WindowAdapter and ActionListener are also named. For windowAdapter, it's found in java.awt.event.WindowAdapter. ActionListener is named in java.awt.event.ActionListener.
Since these are named, I don't see the distinction you're making.
-Peter
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael and Cindy,
I think you are misreading Peter's code. It does appear to illustrate an example of an anonymous inner class. ClassInterfaceTest is not what he is trying to say is an anonymous class; ClassInterfaceTest is the class from which his anonymous class derives. He is creating an object of that anonymous class when he calls the test method:

The text he's printing is false, however.
Peter,
Your example, however, illustrates only an anonymous class derived from a class. It doesn't implement the interface too (except for the fact that its base class does). In order to create an anonymous inner class, you have to do a new Foo(){/*body*/} where Foo is either a class or an interface. Foo can't be both a class and an interface. Michael is correct in that regard.
I think the "abstract" stuff in Peter's example gets in the way of the meat of the discussion, which is about where in the code snippet is the anonymous class.
Susan (my copy of RHE is newer than Peter's)
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Susan,
I think that you are right. After re-reading Peter's example I see that I was looking at the wrong line of code (sorry Peter).
Thanks for clearing it up.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I surprised me at first when Michael says Peter's code doesn't illustrate the problem. And Michael, I'm having your book. It's a good one to study for the exam =).
Thank all.
 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic