Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Implementing an Interface

You use an interface used when you write a class that implement the interface. A class declares all of the interfaces that it implements in its class declaration. To declare that your class implements one or more interfaces, use the keyword implements followed by a comma-delimited list of the interfaces implemented by your class.

For example, consider the Collection interface introduced on the previous page. Now, suppose that you were writing a class that implemented a FIFO queue (a First-In-First-Out queue). Because a FIFO queue object contains other objects it makes sense for the class to implement the Collection interface. The FIFOQueue class would declare that it implements the Collection interface like this:

class FIFOQueue implements Collection {
    . . .
    void add(Object obj) {
        . . .
    }
    void delete(Object obj) {
        . . .
    }
    Object find(Object obj) {
        . . .
    }
    int currentCount() {
        . . .
    }
}
thereby guaranteeing that it provides implementations for the add(), delete(), find(), and currentCount() methods.

By convention, the implements clause follows the extends clause if there is one.

Note that the method signatures of the Collection interface methods implemented in the FIFOQueue class must match the method signatures as declared in the Collection interface.


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces