• 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 Inner class doubt

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic