• 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 classes

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anonymous inner class can extend only one class and can implement only one interface. Please correct me if i am wrong. if not, please do let me know why only one interface or one class
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Anonymous inner class can extend only one interface OR one class. It cannot do both. Just imagine how an anonymous inner class would implement an interface and extend a class?? There is no syntax to support that. You cannot do this

Object obj = new Object() implements Comparable {};

or

Comparable com = new Comparable() extends Number{};

This would be an invalid syntax...
 
aparna shinde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for correcting me. Anonymous class can implement only one interface or can extend only one class. My question is why only one interface?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My explanation was also about that. How would an anonymous inner class implement more than one interface?? There is no syntax to do that. You cannot do this

Object obj = new Comparable() implements Comparator(){/*some code*/};

That would be a compilation error...
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Aparna:
My question is why only one interface?



I guess the doubt arises in your mind because a normal class can implement more than one interface but an anonymous class cannot.

Let me try and answer this question:
The syntax for an anonymous inner class allows us to create on the fly instances of classes/interfaces needed by us.



You can see that with the creation of an implementation of SINGLE interface using anonymous inner class is pretty complex as compared to the other syntax of java.

Also with the syntax made available for anonymous inner classes(AIC) by java creators, it would become even more complicated to allow an AIC to implement multiple interfaces.
I guess they wanted to keep the unnecessary complication aside as it would very rarely be needed in a well designed application to need on the fly instantiation of multiple interfaces in one go.

But, you can achieve that in a different way though:

 
reply
    Bookmark Topic Watch Topic
  • New Topic