Anonymous class is normally used to provide a simple implementation of an adapter class. Many developers don’t just wanna use these because they feel its not really required and is toocomplex.
For sure you may do the required task without using anonymous classes but sometimes it is really wise to use these. Let me list features of anonymous classes for you:
When a local class is used only once, consider using anonymous class syntax. It places the definition and use of the class in exactly the same place.
Syntax
We use new keyword for defining an anonymous class and creating an instance of that class. It is followed by the name of a class and a class body definition in curly braces. If you wish to subclass your anonymous class, then the name following the new keyword should be the name of some superclass. This way the anonymous class is a subclass of the named class.
If you wish your subclass to implement an interface, then the name following new should be an interface. This way your anonymous class implements that interface and extends Object. Remember that the syntax does not include any way to specify extends or implements keyword.
Example
// The anonymous class is defined as part of the return statement
return new MyClass() {
public String getName() {
...
}
};
// Note the required semicolon: it terminates the return statement
Since anonymous class has no name, it is not possible to define a constructor for it within the class body. But if you provide any arguments between the parentheses following the superclass name then they are implicitly passed to the superclass constructor.
For sure you may do the required task without using anonymous classes but sometimes it is really wise to use these. Let me list features of anonymous classes for you:
- is a local class without a name
- is defined and instantiated in a single expression using the new operator
- anonymous class definition is an expression
- it can be included as part of a larger expression (method call)
When a local class is used only once, consider using anonymous class syntax. It places the definition and use of the class in exactly the same place.
Syntax
We use new keyword for defining an anonymous class and creating an instance of that class. It is followed by the name of a class and a class body definition in curly braces. If you wish to subclass your anonymous class, then the name following the new keyword should be the name of some superclass. This way the anonymous class is a subclass of the named class.
If you wish your subclass to implement an interface, then the name following new should be an interface. This way your anonymous class implements that interface and extends Object. Remember that the syntax does not include any way to specify extends or implements keyword.
Example
// The anonymous class is defined as part of the return statement
return new MyClass() {
public String getName() {
...
}
};
// Note the required semicolon: it terminates the return statement
Since anonymous class has no name, it is not possible to define a constructor for it within the class body. But if you provide any arguments between the parentheses following the superclass name then they are implicitly passed to the superclass constructor.
Comments