Java Reference
In-Depth Information
Any class implementing MouseListener needs to implement all five of these
methods. Notice that the MouseListener interface has a parent interface
named EventListener. Any class that implements MouseListener must also
implement the methods of EventListener; however, EventListener does not
contain any methods:
package java.util;
public interface EventListener
{}
The EventListener interface is an example of a tagging interface , which is dis-
cussed in a sidebar later in this chapter.
The EventListener interface is the parent class of all listener interfaces
(such as MouseListener). The listener interfaces are used extensively in
handling GUI events, discussed in Chapter 13, “GUI Components and
Event Handling.” Listener interfaces are also an essential element of
JavaBeans, discussed in Chapter 19, “JavaBeans.”
User-Defined Interfaces
You can also write your own interfaces. The following PhoneHandler interface
is a user-defined interface that contains three methods:
package customer.service;
public interface PhoneHandler
{
public void answer();
public boolean forward(int extension);
public void takeMessage(String message, String recipient);
}
The PhoneHandler interface must be saved in a file named PhoneHandler
.java, and the bytecode file PhoneHandler.class needs to appear in a \customer\
service directory.
Let's work through an example where you write your own interface and
then a class that implements it. Open your text editor and follow along
through the following steps.
Search WWH ::




Custom Search