Anonymous Inner Classes
Except the inner class, there are two types of supplementary inner classes :
The inner class which is declared inside the body of a method is known as the local inner classes. The class declared inside the body of a method without naming it is known as anonymous inner classes.
Here we are discussing anonymous inner classes.
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.
Consider the following code :
button.addActionListener(new ActionListener() { public void actionPerfored(ActionEvent e) { // do something. } });
As you can see in the above code, i don't need to create an extra class that implements ActionListener . I initiated anonymous inner class(here it is new ActionListener()) without creating a separate class. You can also perform method overloading by implementing anonymous inner classes.