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.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Anonymous Inner Classes - Anonymous Inner Classes tutorial
Post your Comment