The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Interfaces and Packages

Creating and Using Interfaces

The What Is an Interface? (in the Learning the Java Language trail) section provided an introduction to interfaces. This section goes further, showing you how to create and to use interfaces and talking about why you would use an interface instead of a class.

An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy; it also defines a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior.


Definition: An interface is a named collection of method definitions, without implementations.

Because an interface is simply a list of unimplemented, and therefore abstract, methods, you might wonder how an interface differs from an abstract class. The differences are significant.

Let's set up the example we'll be using in this section. Suppose you have written a class that can watch stock prices coming over a data feed. This class allows other classes to register to be notified when the value of a particular stock changes. First, your class, StockApplet, would implement a method that lets other objects register for notification, as follows.

public class StockMonitor {
     public void watchStock(StockWatcher watcher,
                            TickerSymbol tickerSymbol,
                            BigDecimal delta) {
     ...
     }
}
The first argument to this method is a StockWatcher object. StockWatcher is the name of an interface whose code you will see in the next section. That interface declares one method: valueChanged. An object that wants to be notified of stock changes must be an instance of a class that implements this interface and thus implements the valueChanged method.

The other two arguments provide the symbol of the stock to watch and the amount of change that the watcher considers interesting enough to be notified of. When the StockMonitor class detects an interesting change, it calls the valueChanged method of the watcher.

The watchStock method ensures, through the data type of its first argument, that all registered objects implement the valueChanged method. It makes sense to use an interface data type here because it only matters whether registrants implement a particular method. If StockMonitor uses a class name as the data type, that would artificially force a class relationship on its users. Because a class can have only one superclass, it would also limit what type of objects can use this service. By using an interface, the registered objects class could be anything (for example, Applet or Thread), thus allowing any class anywhere in the class hierarchy to use this service.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.