The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Classes, Interfaces, and Packages

## Implementing the Sleeper Interface

> An interface defines a protocol of behavior. A class that implements an interface adheres to the protocol defined by that interface. To declare a class that implements an interface, include an `implements` clause in the class declaration. Your class can implement more than one interface (the Java platform supports multiple interface inheritance), so the `implements` keyword is followed by a comma-separated list of the interfaces implemented by the class.

> > By Convention: The `implements` clause follows the `extends` clause, if it exists.

> The `GUIClock` class implements the `Sleeper` interface, which contains a single method declaration for the `wakeUp` method. Thuse `GUIClock` must implement the `wakeUp` method:

> >     public class GUIClock extends Applet implements Sleeper {
> >         . . .
> >         public void wakeUp() {
> >             // update the display
> >         }
> >     }

> Remember that when a class implements an interface, it is essentially signing a contract. The class must provide method implementations for all of the methods declared in the interface and its superinterfaces. Or, the class must be declared `abstract`. The method signature (the name and the number and type of arguments) for the method in the class must match the method signature as it appears in the interface.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form