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.
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.
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.
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
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:
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]
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.