• 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

Double-standard for anonymous inner class extending directly from Object class

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enthuware explanation for answer to question:

When you create an anonymous class for an interface then it extends from Object. For example,





So the anonymous inner class is instance of ActionListener() which is an interface, hence the anonymous inner class directly extends from Object.


But if you make an anonymous class from another class then it extends from that class. For example, consider the following class:






Here the anonymous class actually extends from MyListener class and successfully overrides the actionPerformed() method.



So anytime an anonymous inner class is made from another class (not an interface) then it directly extends that class.

Is my understanding correct?
 
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
All classes in java extend from Object class either directly or indirectly. When a class doesn't extend any other class, it implicitly extends Object class. This also applies to anonymous inner classes...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:All classes in java extend from Object class either directly or indirectly. When a class doesn't extend any other class, it implicitly extends Object class. This also applies to anonymous inner classes...




I'm going to revisit this. In Enthuware Generics and Collections questions this concept came up (in the form of instanceof operator) and I wasn't able to grasp. When I re-visit these questions, I think I shall understand.

Will mark this thread resolved for now.

Thank you!
 
reply
    Bookmark Topic Watch Topic
  • New Topic