سؤال

My class header:

public class GraphEdge implements Comparable<GraphEdge>{

/** Node from which this edge starts*/
protected Point from;
/** Node to which this edge goes*/
protected Point to;
/** Label or cost for this edge*/
protected int cost;

My compareTo method:

@Override
public int compareTo(GraphEdge other){
    return this.cost-other.cost;
}

but Eclipse gives me the error:

The method compareTo(GraphEdge) of type GraphEdge must override a superclass method

whyyyyy? I tried just doing Comparable, with

@Override
public int compareTo(Object o){
            GraphEdge other = (GraphEdge) o;
    return this.cost-other.cost;
}

but this also failed.

هل كانت مفيدة؟

المحلول

Most likely your project is set to Java 1.5 compliance level - try setting it to 1.6 and it should work. Don't have Eclipse here to test, however I remember that when set to 1.5 I could not use @Override on interface (but could on class) method overriding. This worked OK when set to 1.6.

I.e. this should fail when set to 1.5, but work OK when on 1.6:

interface A {
   void a();
}

class B implements A {
   @Override
   public void a() {
   }
}

So try it:

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top