An interface consists of one or more method signatures, but it does not contain any code implementing any
of the methods. (An interface can include constants as well as method signatures, but we will focus on the methods.)
A method signature is like the first line of a method; the signature specifies what the method will return,
the name of the method, and what arguments the method expects when it is called.
For example, Java provides the interface Comparable, and the Comparable interface specifies a single
method called compareTo(). The compareTo() method expects an object as an argument (the object with
which to compare the first instance) and it returns an int. The value of the returned int will be 0 if the two
objects being compared are equal, -1 if the first object is ???smaller??? (should be ordered first), and +1 if the first
object is ???larger??? (should be ordered second).
We can implement the interface Comparable in our Automobile class by adding this code to our class
Automobile:
class Automobile implements Comparable
{
.
.
public int compareTo( Automobile car )
{
return this.toString().compareTo( car.toString() );
}
The new first line of our Automobile class now declares that the class Automobile will implement
the interface Comparable.
Pages:
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229