There is a good deal of "magic" involved in how an event is actually dispatched. In general, the
processEventTypemethod in the component object calls a particular method in the receiving object appropriate for the particular type of event. Thus, different methods are called, for example, when the mouse is moved (button up) versus when it is dragged (button down). How can the component object know which methods in the receiving object to call?The answer lies in the concept of a Java Interface. Much has been made of the fact that Java does not allow multiple inheritance. Thus, a given class can be the subclass of at most one other class. It cannot inherit from two classes. However, it is able to achieve some of the benefits of multiple inheritance thorough
Interfaces. An interface is a specification for a set of methods and variables. A class can elect toimplementthe interface, committing it to including within its definition all of the methods and variables specified in the interface. Then, if some method in another class wishes to communicate with such an object, it can do so through the methods or variables designated in the interface since it knows that they will be present in the object.
Inheritance vs. Interface.
Thus, in the
Buttonclass, theprocessActionEventmethod can communicate with all registered objects through anactionPerformedmethod that is included in each such object by virtue of their commitments to implementing theactionListenerinterface.The complete set of event interfaces for AWT components is included in the
java.awt.eventpackage.