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 (first in, first out) queue. Because a FIFO queue object contains other objects it makes sense for the class to implement theCollection
interface. TheFIFOQueue
class would declare that it implements theCollection
interface 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() { . . . } }Collection
interface, theFIFOQueue
class guarantees that it provides implementations for theadd
,delete
,find
, andcurrentCount
methods.By convention, the
implements
clause follows theextends
clause if it exists.Note that the method signatures of the
Collection
interface methods implemented in theFIFOQueue
class must match the method signatures as declared in theCollection
interface.
Objects, Classes, and Interfaces |