There is a good deal of "magic" involved in how an event is actually dispatched. In general, the
processEventType
method 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 toimplement
the 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
Button
class, theprocessActionEvent
method can communicate with all registered objects through anactionPerformed
method that is included in each such object by virtue of their commitments to implementing theactionListener
interface.The complete set of event interfaces for AWT components is included in the
java.awt.event
package.