File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Comparator giving compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparator giving compiler error" Watch "Comparator giving compiler error" New topic
Author

Comparator giving compiler error

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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.



When The Going Gets Tougher,The Tougher gets Going
Federico Cardelle
Greenhorn

Joined: Jul 26, 2011
Posts: 26
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

Joined: Dec 08, 2008
Posts: 4

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


~ Chandrakant Talele
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator giving compiler error