Originally posted by Peter Tran:
[B]Bhakti,
In your example, you're instantiating an anonymous class from the WindowAdapter class and you provide an override method for the windowClosing method. This is perfectly fine, because you are not keeping a reference of the WindowAdpater class around.
E.g.
The second example is fine, because I'm creating a reference to an anonymous class. Remember, that this anonymous class is a concrete class because I'm providing an implementation for one of the methods in the WindowAdapter class.
I hope this makes sense. I realize the concept is confusing, and takes some getting use to.
-Peter[/B]
Originally posted by Golam Newaz:
Hi,
In your example:
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
});
// new WindowAdapter(). It says to compiler" Create an instance
of a new, unamed class which implements the WindowListener
interface...
Look here you are making an instance using new but without any object referece name . So it is unnamed class. So abstract
can be anonymous class.
Whatever you say after the "new" as in "new Something()", if Something is an interface, then the anonymous
class implements that interface (and must define all the
methods of that interface). But if Something is a class,
then your anonymous class automatically becomes a subclass of that class. This is perfect for the event adapter
classes like WindowAdapter.
- Golam Newaz
Don't get me started about those stupid light bulbs. |