Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator giving compiler error

 
Deepak Lal
Ranch Hand
Posts: 561
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have written a FruitNameComparator and FruitQuantityComparator as shown below.



Error is as below.
Main.java:3: Fruit is not abstract and does not override abstract method compareTo(Fruit) in java.lang.Comparable
class Fruit implements Comparable<Fruit>{
^
1 error

i dont know where is the error and i have written a comparator as illustrated in the example above.whats wrong with the code.Could you please help me to solve this java problem.

 
Federico Cardelle
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler expects you to implement a method

like it is explained in
http://download.oracle.com/javase/7/docs/api/java/lang/Comparable.html
 
Chandrakant Talele
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need to understand difference between Comparable and Comparator.

Comparable implementations provide a natural ordering for a class, which allows objects of that class to be sorted automatically.

Comparator is an object that encapsulates an ordering. You will use Comparator when you want to sort some objects in an order other than their natural ordering or when you want to sort some objects that don't implement Comparable.

Refer to http://download.oracle.com/javase/tutorial/collections/interfaces/order.html where this stuff is explained nicely.

In your case, Fruit is not required to implement Comparable. This should solve the problem
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic