java - Comparable and compareto -


i have problem. have class extends class , implements comparable. when tried compile program had error: flight not abstract , not override abstract method compareto. anyway, code of flight this:

class flight extends keyflight implements comparable<flight>{   public keyflight kf;  public boolean departure;   public void flight(){  keyflight kf=new keyflight();  boolean departure=false;  }   public int compareto(keyflight other){   if (kf.time==other.time){   if (kf.key<other.key){    return kf.key;   } else {    return other.key;   }   } else {   if (kf.time <other.time){    return kf.key;   } else {    return other.key;   }    } } } 

thank in advance!

you should use

implements comparable<keyflight> 

instead of

implements comparable<flight> 

as want compare 2 keyflights rather flights itself. type of parameter defines in compareto, should match type specified in implements clause going compare with.

another issue code is, in constructor re-defining keyflight, should be

public void flight(){     kf=new keyflight(); 

else nullpointerexception in future. same applies departure boolean.

a side note, default in java boolean initialised false, don't have explicitly in constructor.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -