Question

I see code like this

class A implements Comparable<A> {


}

What does this mean, what are the advantages and disadvantages of it?

Was it helpful?

Solution

It means that class is committed to respond to the methods defined by the "interface" Comparable.

The advantage you have with this ( and any other "implements" declaration ) it that you can "abstract" the type of the object and code to the interface instead.

Consider this

class A implements Comparable {
    .... 
}

class B implements Comparable {
    .... 
}


class C implements Comparable {
    .... 
}

You then may code something that can use Comparable instead of a specific type:

public void doSomethingWith( Comparable c ) {

       c.compareTo( other ); // something else...

}

And invoke it like:

  doSomethingWith( new A() );

  doSomethingWith( new B() );

  doSomethingWith( new C() );

Because you don't really care what the type of the class is, you just care it does implement the interface.

This ( program to the interface rather to the implementation ) is one of the most powerful techniques in OO programming world, because it promotes low-coupling.

OTHER TIPS

Implementing a comparable interface means that A can be compared with other instances of A.

Many operations in java that involve sorting use the methods defined in the Comparable interface to determine if instances of A are greater then less or equal to other instances.

By implementing these methods you are able to use a lot of handy features such as java sort, use instances of A as keys for binary trees, and more.

It means that class A can be sorted using the Comparable compareTo method:

A a1 = new A(1);
A a2 = new A(3);

// -1, 0, or 1 depending on whether a2 is less than, equal to, or greater than a1
int order = a1.compareTo(a2);

Comparable uses the natural ordering for your class.

Another way to go since Java 5 is Comparator. You can pass this object around and have more than one way to compare and sort the target class. For example, sometimes you might want to sort a Name class by first name, other times by last name. Comparable only gives you one way to do it, but you can have several Comparator instances.

It means that the class is one which can be operated on by functions which expect their arguments to be objects that can be compared with other objects of the same type (such as the pre-defined sorting functionality for lists).

Implementing the Comparable interface means that the class supports certain functions which the interface requires (specifically, the compareTo() method), that the sorting (or other) operations being performed on the class will utilize to do their work without having to care about the rest of the class.

For more details: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

In addition to what everyone else said, by implementing an interface (or extending a class), you get compiler enforcement of the contract of the supertype. In the case of 'Comparable', that means that you get a compiler error if you fail to implement the 'int compareTo(A anA)' method in the implementing class. Adding the annotation '@Override' to the implementing method provides even more compile-time safety; if you fail to implement the method with the right signature the compiler will tell you. Compile-time errors are much, much easier and cheaper to fix than run-time errors. Furthermore, implementing an interface allows any instance of an implementing class to be treated as the interface type for methods (and constructors) that take the interface type as an argument or generic parameter. For example, the 'java.util.Collections.max(Collection coll)' method takes a collection whose base type must extend 'Comparable'.
http://download.oracle.com/javase/7/docs/api/java/util/Collections.html#max(java.util.Collection)

It means that objects of this class can be easily sorted in collections because they can be compared to each other. The other option is to implement a Comparator which is a class responsible for sorting other classes. The Comparable puts the sorting logic directly in the class to be sorted; the Comparator puts the sorting logic in a different class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top