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


Implementing an 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 (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() {
        . . .
    }
}
By declaring that it implements the Collection interface, the FIFOQueue class guarantees that it provides implementations for the add, delete, find, and currentCount methods.

By convention, the implements clause follows the extends clause if it exists.

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