1

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.

Colleen
  • 23,899
  • 12
  • 45
  • 75

1 Answers1

7

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

icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • More likely providing a duplicate answer from another question here on SO should be avoided :) Check my comment on OP's question.. – Jack Mar 02 '12 at 02:53
  • @Jack What's interesting is that answer says that it's not **supported** on interface methods on 1.5, however I just tested myself with `javac -source 1.5 -target 1.5` and it compiled OK - true, with javac v1.6.0_26-b03, however the problem seems a bit more ambiguous. And to note - I did run into this myself, but was never sure whether that was Eclipse issue or Java issue. – icyrock.com Mar 02 '12 at 02:58
  • My working code got bit by the 1.5 compliance level on my new Eclipse install. After changing it to 1.6 per icyrock, I also had to change my project Facet version to 1.6 as well, see http://stackoverflow.com/questions/2239959/faceted-project-prblem-java-version-mismatch-error-message. – Ken Lin Sep 07 '14 at 03:40