Anonymous 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.
class popcorn
{
public void pop
{
System.out.println("popcorn");
}
}
class food
(
popcorn p = new popcorn()
{
public void pop
{
System.out.println("anonymous popcorn");
}
};
)
Example 2:
Note:
class popcorn
{
public void pop
{
System.out.println("popcorn");
}
}
class food
(
popcorn p = new popcorn()
{
public void pop
{
System.out.println("anonymous popcorn");
}
};
)
Example 2:
button.addActionListener(new ActionListener() { public void actionPerfored(ActionEvent e) { // do something. } });
Note:
- Anonymous inner class have no name,and theire type must be either a subclass of the name type or the implementor of the named interface.
- An Anonymous inner class always end with }; or
});
- the key to remember is that the class is always define with in a method arguments.
Comments
Post a Comment