Anonymous Inner Classes - A Very
Brief Introduction
Java allows us to define a class as a member of another class:
Syntax:
class OuterClass
{
...
class InnerClass
{
...
}
}
An instance of InnerClass is contained within an instance of
OuterClass.
You are not required to name the inner class - in this case we say the
class is an anonymous inner class.
Example:
interface PersonProperties
{
public String getName();
public int getAge();
}
class Person
{
...
public PersonProperties getInfo()
{
return new PersonProperties() //
an instance of an unnamed class is returned - the unnamed class
implements interface PersonProperties
{ // beginning of unnamed class
definition
public String
getName()
{
return name;
}
public int
getAge()
{
return age;
}
} // end of unnamed class definition
} // end of method getInfo()
}
Syntax:
new InterfaceType() { methods and data}
or more generally,
new SuperType(construction parms)
{
// inner class methods and data
}
The SuperType can be a class or interface. If SuperType is a class,
then the anonymous inner class extends that class.