|
|
Objects, Classes, and Interfaces |
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 keywordimplementsfollowed by a comma-delimited list of the interfaces implemented by your class.For example, consider the
Collectioninterface 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 theCollectioninterface. TheFIFOQueueclass would declare that it implements theCollectioninterface like this:By declaring that it implements theclass FIFOQueue implements Collection { . . . void add(Object obj) { . . . } void delete(Object obj) { . . . } Object find(Object obj) { . . . } int currentCount() { . . . } }Collectioninterface, theFIFOQueueclass guarantees that it provides implementations for theadd,delete,find, andcurrentCountmethods.By convention, the
implementsclause follows theextendsclause if it exists.Note that the method signatures of the
Collectioninterface methods implemented in theFIFOQueueclass must match the method signatures as declared in theCollectioninterface.
|
|
Objects, Classes, and Interfaces |