You are on page 1of 1

The anonymous inner classes is very useful in some situation.

For example
consider a situation where you need to create the instance of an object without
creating subclass of a class and also performing additional tasks such as method
overloading.

When using local inner classes, you can often go a step further. If you want to make only a
single object of this class, you dont even need to give the class a name.
Such a class is called an anonymous inner class. Usually the inner class extend some interface
or extend other class.
This syntax for anonymous classes is very cryptic.
new SuperType(construction parameters) {
inner class methods and data
}
Here, SuperType can be an interface, such

as ActionListener; then, the inner class


implements that interface. Or SuperType can be a class; then, the inner class extends
that class.

An anonymous inner class cannot have constructors because the name of a constructor
must be the same as the name of a class, and the class has no name. Instead, the
construction parameters are given to the superclass constructor. In particular,
whenever an inner class implements an interface, it cannot have any construction
parameters. Nevertheless, you must supply a set of parentheses as in

new InterfaceType () { methods and data }

You might also like