views:

322

answers:

5

I see code like this

class A implements Comparable<A> {


}

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

+1  A: 

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

Amber
A: 

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.

cliff.meyers
+1  A: 

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.

duffymo
+1  A: 

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.

Ori Cohen
+2  A: 

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.

OscarRyz