Saturday, May 7, 2011

INTERFACES

In Java, we can also specify an interface to enforce a common set of behaviors among different classes.
For example, think about the problem of sorting a group of Automobile objects. How should Automobiles
be ordered? By year? Alphabetically by make? By horsepower? This problem arises frequently when we’re
working with objects, so the Java language specifies an interface that any new class can implement in order
to facilitate sorting instances of the new class.
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<Automobile> {
.
.
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. The <Automobile> syntax says that we will only be comparing an Automobile
object with another Automobile. If, by some error of programming, our program tries to compare an
Automobile object with a Thermostat object, the JVM will generate an error.
The new compareTo() method implements the Comparable interface for the class Automobile.
We’ve taken a shortcut here which takes advantage of the toString() method we already have for
Automobile. The phrase this.toString() will return a String object which represents this instance
of an Automobile. The key word this always references the particular instance itself. The String
returned will be of this form:
2002 VW Passat
The year will be followed by the make and the model of the Automobile. Likewise, the phrase
car.toString() will return a String representing the other Automobile, such as this:
2006 Kia Rio
The String class has a compareTo() method that orders Strings in “ASCIIbetical” order, which is
like alphabetical order, except that it follows the ASCII encoding values of characters. In ASCII the letters are
coded in alphabetical order, but all uppercase letters come before any of the lower-case letters, and digits and
symbols are included as well as letters. For our purposes of ordering Automobiles, we thought that the
toString() representation of Automobiles sorted in ASCIIbetical order would be fine. Older
cars will be ordered first, and among cars of the same year, cars will be sorted ASCIIbetically by make and
model.
The interface idea is similar to the idea of a class, in that an interface creates a new data type. When
the class Automobile implements the interface Comparable, instances of Automobile can also be
treated as instances of Comparable. Just as good design of a class hierarchy can reduce programming and
improve reliability, good interface design can also.
For instance, the sort() method of the Java Collections class will sort lists of Comparable
objects. Our Automobile class implements Comparable, Java’s String class implements Comparable,
Java’s Date class implements Comparable, etc. One sort() method will work for any group of objects
whose class implements Comparable. That alone is a great example of code reuse.
It’s easy to design and use your own interfaces, too. However, in this brief chapter we will not be discussing
that topic

No comments:

Post a Comment