• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Comparator giving compiler error  RSS feed

 
Ranch Hand
Posts: 561
Eclipse IDE Java
  • 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.

 
Greenhorn
Posts: 26
  • 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
 
Greenhorn
Posts: 4
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!