• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous class....  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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.
 
gunjan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks.
Gunjan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!