Silly question on anonymous class
Hi,
While using Adapter classes in Swing applications, we use the keyword "new" for an adapter class. And an adapter class is an abstract class.
We cannot create an instance of abstract class but how are we using new WindowAdapter()?
Please have a look at this code
publicstaticvoid main( String args[] )
{
MouseTracker app =new MouseTracker();
app.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing( WindowEvent e )
{
System.exit( 0 );
}
} );
}
Can someone explain me please?
It's not an abstract class, and it's not a WindowAdapter either. It's an anonymous class, as your subject line says, which extends WindowAdapter, and it contains an implementation of the method which was abstract in WindowAdapter, so it's not abstract.
ejpa at 2007-7-14 0:33:20 >

"An anonymous class is essentially a local class without a name." - Java In A Nutshell
(That assumes you know what a local class is).
The answers above are quite clear, so maybe some [url=http://www.google.co.uk/search?hl=en&safe=off&q=anonymous+class+java&btnG=Search&meta=]Google Results[/url] would help?
[url=http://www.developer.com/java/other/article.php/3300881]This looks quite good[/url], as does [url=http://mindprod.com/jgloss/anonymousclasses.html]this one[/url].
mlka at 2007-7-14 0:33:20 >
