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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i know an anonymous class implements only an interface
and all classes implicitly/explicitly extend class "Object".
Anonymous classes are same as other classes except that they are defined and instantiated in the same place without a name.
My question, is this statement true or false.
An anonymous inner class is always assumed to extend Object.

Thanks.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I feel Anonymous class are like local inner class, except without a name. Also they have no explicit constructor.
What I am not sure about is definietion of anonymous classes outside a method. I feel anonymous classes can be defined locally within a method only.
 
Wanderer
Posts: 18671
  • 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 it may directly extend a class. If it implemets an interface, then it also directly extends Object (though this is not written explicitly). If it extends a class, then that also indirectly extends Object because all classes indirectly extend Object.
Outside of a method, anonymous classes (and local classes) can also be used in static initializers, and in initializers for class or instance variables.
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim:
To add to what you said: Anonymous classes cannot extend and implement both at the same time. Right?
Can you provide a example , of anonymous class being defined inside a intializers of instance variables.
Thanks
GUnjan
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sort of. If an anonymous class implements an object, then the only class it extends is Object - you cannot specify a class to extend and an interface to implement.
As an example of an instance variable initializer, outside a method:

Not very useful, but legal.
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Gunjan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim:
How can local classes be defined outside a class.

Originally posted by Jim Yingst:
Outside of a method, anonymous classes (and local classes) can also be used in static initializers, and in initializers for class or instance variables.[/B]


 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They can't. I gave an example for inside a class but outside a method; outside a class, no nested classes are possible (by definition really), and that includes local classes.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic