aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner class doubt 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 "Anonymous Inner class doubt" Watch "Anonymous Inner class doubt" New topic
Author

Anonymous Inner class doubt

Yogesh Mashalkar
Greenhorn

Joined: Sep 20, 2005
Posts: 25
Hi,
for the code below can the statement mentioned in bold be "true"

Anonymous Inner class can extend one class and can implement multiple interfaces



or is it that in case of anonymous inner classes only one of the two things hold true,that it either extends a class or implements an interface??

irrespective of whether its super class has unimplemented interface methods(i.e super class is abstract)..

thnx for the help!
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Yogesh,

There's no reason why you shouldn't be able to use a class which extends another class and implements multiple interfaces as class type for an anonymous inner class . There are some differences in using anonymous inner classes in contrast to regular classes but I don't know of any restriction regarding inheritance or interface implementation.

But your code example doesn't even use an anonymous class. Your holding a reference "t" for what you suppose to be an anonymous class. You can see lots of examples of using anonymous inner classes in Swing code regarding event listeners. There you often instantiate an anonymous class with operator "new" directly where it is consumed as a parameter to a method without holding a reference to it.

Perhaps I missed your question but I don't really see what's the problem here with anonymous inner classes?!?

Marco
Yogesh Mashalkar
Greenhorn

Joined: Sep 20, 2005
Posts: 25
i was just solving one of the K&B exercises for Inner Classes..
the question was which of the following are true for anonymous inner class?
1.it can extend exactly one class and implement exactly one interface
2."" "" "" "" "" "" "" can implement multiple interfaces
3."" "" "" "" "" "" or implement exactly one interface
4.some clearly wrong option
5.some clearly wrong option

the correct answer given is option 3..that's what i chose too

i was trying to verify if option 2 can be true??
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

I'm still convinced that you can create an anonymous class of every class or interface type. So in my opinion it clearly could be any class which extends another class and implements multiple interfaces.

You can also directly instantiate an interface with "new" and implement this interface in-place. Then of course it can only be one interface. Perhaps it's this what the question tries to find out... or I am really wrong with this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner class doubt