• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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.
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic