Objects and Classes in Java |
In the previous section,Spot
contains empty implementations because it isn't interested in all of the kinds of mouse events. In this section, we rewriteSpot
so that those empty methods aren't necessary. This can be done using an inner class to implement an adapter. An inner class is one kind of nested class (a class that is defined within another class). To find out more about nested classes, read Implementing Nested Classes.The font changes in this new version of the applet show how this version of the class differs from that in the previous section:
import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AdapterSpot extends Applet //no implements clause { private Point clickPoint = null; private static final int RADIUS = 7; public void init() { addMouseListener(new MyMouseAdapter()); } public void paint(Graphics g) { g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); if (clickPoint != null) g.fillOval(clickPoint.x-RADIUS, clickPoint.y-RADIUS, RADIUS*2, RADIUS*2); } class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent event) { clickPoint = event.getPoint(); repaint(); } } /* no empty methods! */ }Here's the running applet:
Now, instead of implementing the interface itself,
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1-compliant browser, such as HotJava or the JDK Applet Viewer (appletviewer
).AdapterSpot
declares an inner class,MyMouseAdapter
.MyMouseAdapter
is a subclass ofMouseAdapter
, which implements theMouseListener
interface.MouseAdapter
provides empty implementations for all of the methods declared inMouseListener
. Through the use of this inner class, theAdapterSpot
class can write only the method for the one type of event it is interested in:mousePressed
.You will see a lot of inner classes, and even implement some, if you plan to handle events from the AWT.
Anonymous Classes
In the previous example, the name of the inner class adds nothing to the meaning of the program. You can forego naming an inner class and just define it where it's used. This is called an anonymous class.
Definition: A class without a name is called an anonymous class.
Here's yet another version of the applet, rewritten to use an anonymous class (font changes show the modifications):
import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class AnonymousSpot extends Applet { private Point clickPoint = null; private static final int RADIUS = 7; public void init() { addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) { clickPoint = event.getPoint(); repaint(); } } ); } /* paint method not shown */ }And here's the running applet:
You should restrict your use of anonymous classes to those classes that are for a single use and very small (no more than a method or two). Otherwise, you may jeopardize the readability of your code.
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1-compliant browser, such as HotJava or the JDK Applet Viewer (appletviewer
).
Objects and Classes in Java |