This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparator giving compiler error" Watch "Comparator giving compiler error" New topic

Comparator giving compiler error

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 561

i have written a FruitNameComparator and FruitQuantityComparator as shown below.

Error is as below. 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

Joined: Jul 26, 2011
Posts: 26
The compiler expects you to implement a method

like it is explained in
Chandrakant Talele

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 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:
subject: Comparator giving compiler error
It's not a secret anymore!