Using Interface Callbacks for GUI Event Handlers

So far we've discussed examples of the Comparable and Cloneable interfaces. These interfaces solve compile time issues of forcing a class to have certain behavior. There is an additional way that an interface can be used, to obtain more dynamic behavior. It forms the basis of GUI programming in Java. We have already seen most of this; the only piece that's new is the way that the object-which-implements-an-interface tells the servicing class about itself.

In our earlier example, we called a service method and passed an instance of our handler object as an argument:

Depot.service( myPlane );

Inside the body of service, the method myPlane.refuel() was immediately called back, and that really ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.