java - Human is not abstract and does not override abstract method compareTo(Human) .solution? -


public class human implements comparable<human> {     private int age;     private string name;      public human(string givenname, int age) {         this.name = givenname;         this.age = age;     }      public string getname() {         return name;     }      public int getage() {         return age;     }      public string introduce() {         return "hey! i'm " + name + " , i'm " + age + " years old.";     }      public int compareto(human h1, human h2) {             int hum1 = h1.getage();             int hum2 = h2.getage();             system.out.println(hum1 - hum2);     } } 

this code sorting arraylist using age parameter , error msg appears

./human.java:6: error: human not abstract , not override abstract method compareto(human) in comparable public class human implements comparable

what's wrong here? please.

you want change:

public int compareto(human h1, human h2) 

to:

public int compareto(human h_other) 

two things: first, "c" lowercase. secondly, compareto method compares this human, it's equivalent (using old code):

public int compareto(human h_other) {     return compareto(this, other); } 

Comments