Skip to main content

Section 6.6 Interfaces

Lets turn our attention to making a list of fractions sortable by the standard Java sorting method Collections.sort. In Python, we would just need to implement the __cmp__ method. But in Java we cannot be that informal. In Java, things that are sortable must be Comparable. Your first thought might be that Comparable is superclass of Number, but that is actually not the case. Java only supports single inheritance, that is, a class can have only one parent. Although it would be possible to add an additional layer to the class hierarchy it would also complicate things dramatically, because not only are Numbers comparable, but Strings are also Comparable as would many other types. For example, we might have a Student class and we want to be able to sort students by their GPA. But Student might already extends the class Person for which there would be no natural comparison method.
Java’s answer to this problem is the Interface mechanism. Interfaces are like a combination of “inheritance” and “contracts” all rolled into one. An interface is a specification that says any object that claims it implements this interface must provide the following methods. It sounds a little bit like an abstract class, however it is outside the inheritance mechanism. You can never create an instance of Comparable. Many objects, however, do implement the Comparable interface. What does the Comparable interface specify?
The Comparable interface says that any object that claims to be Comparable must implement the compareTo method. Here is an excerpt from the official documentation for the compareTo method as specified by the Comparable interface. Listing 6.6.1 shows the excerpt.
Listing 6.6.1.
int compareTo(T o)
Compares this object with the specified object for order. Returns a
negative integer, zero, or a positive integer as this object is less
than, equal to, or greater than the specified object. The
implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for
all x and y. (This implies that x.compareTo(y) must throw an exception
iff y.compareTo(x) throws an exception.)
...
To make our Fraction class Comparable we must modify the class declaration line as shown in Listing 6.6.2.
Listing 6.6.2.
public class Fraction extends Number implements Comparable<Fraction> { // fraction class is a child of Number and implements the Comparable interface
    ...
}
The specification Comparable<Fraction> makes it clear that Fraction is only comparable with another Fraction. The compareTo method could be implemented as shown in Listing 6.6.3.
Listing 6.6.3.
public int compareTo(Fraction other) { // compare this fraction with another fraction
    Integer num1 = this.numerator * other.getDenominator();
    Integer num2 = this.denominator * other.getNumerator();
    return num1 - num2;
}

Checkpoint 6.6.4.

Rearrange the blocks to create a Bicycle class that implements Comparable<Bicycle>. The class should feature a private double weight field and a compareTo method that compares bicycles based on their weight.
You have attempted of activities on this page.