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


Using an Interface

An interface gets used when a class claims to implement that 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 Countable interface introduces on the previous page. Now, suppose that you were writing a class that implemented a FIFO queue. Because the FIFO queue class is a collection object (an object that contains other objects) it makes sense for it to implement the Countable interface. The FIFOQueue class would declare that it implements the Countable interface like this:

class FIFOQueue implements Countable {
    . . .
}
thereby guaranteeing that it provides implemenations for the incrementCount(), decrementCount(), currentCount(), and setCount() methods.

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

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


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