Java Reference
In-Depth Information
13.1
Interfaces
Autonomy of Syntax
A linguistic concept attributed to Noam Chomsky
In this section we describe
An interface is a type that groups together a num-
ber of different classes that all include method definitions for a common set of method
headings.
interfaces.
Interfaces
interface
An
An interface is not
a class. It is, however, a type that can be satisfied by any class that implements the interface.
interface
is something like the extreme case of an abstract class.
An interface is a property of a class that says what methods it must have.
An interface specifies the headings for methods that must be defined in any class
that implements the interface. For example, Display 13.1 shows an interface named
. Note that an interface contains only method headings. It contains no
instance variables nor any complete method definitions. (Although, as we will see, it
can contain defined constants.)
To
Ordered
implementing
an interface
implement an interface
, a concrete class (that is, a class other than an abstract
class) must do two things:
1. It must include the phrase
Interface_Name
implements
at the start of the class definition. To implement more than one interface, you list
all the interface names, separated by commas, as in
implements SomeInterface, AnotherInterface
Display 13.1 The Ordered Interface
Do not forget the semicolons at
the end of the method headings.
1 public interface Ordered
2{
3
public boolean precedes(Object other);
4 /**
5 For objects of the class o1 and o2,
6 o1.follows(o2) == o2.preceded(o1).
7 */
8 public boolean follows(Object other);
9}
Neither the compiler nor the run-time system will do anything to ensure that this comment
is satisfied. It is only advisory to the programmer implementing the interface.
 
Search WWH ::




Custom Search