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 keywordimplements
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:
thereby guaranteeing that it provides implementations for theclass FIFOQueue implements Collection { . . . void add(Object obj) { . . . } void delete(Object obj) { . . . } Object find(Object obj) { . . . } int currentCount() { . . . } }add()
,delete()
,find()
, andcurrentCount()
methods.By convention, the
implements
clause follows theextends
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.
Objects, Classes, and Interfaces |