This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes compare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare" Watch "compare" New topic


yael buc

Joined: Nov 17, 2010
Posts: 2

i've got this error:
<anonymous strategies.BasicRational$1> is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator

for this code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


Collections.sort(foodsRef,new Comparator(){
public int compare(Food f1,Food f2) {
Double value1 = f1.getGain() / (position.distance(f1.getPosition()) + f1.getWeight());
Double value2 = f2.getGain() / (position.distance(f2.getPosition()) + f2.getWeight());
return value1.compareTo(value2);

return foodsRef.get(0);


can somebody explain me the problem?

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

"yaeli bb", please check your private messages regarding an important administrative matter.

Thank you.
Jason Cone

Joined: Jul 21, 2004
Posts: 25
Your compare method needs to specify Object arguments, rather than Food arguments.
yael buc

Joined: Nov 17, 2010
Posts: 2
sorry that i didn't mention it:

import game.Food;

food is a class in my code, therefor it's an object.. am i wrong?
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
You are doing an overload, not an override. Important difference.

Also you can satisfy the compiler by using generics (assuming you are at least Java 5 or newer):

Now you're happy, compiler's happy...we're all one big happy ball of warm fuzziness.
I agree. Here's the link:
subject: compare
It's not a secret anymore!