Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparable Interface

 
Jayaram Subramanian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why am i getting error "Person is not abstract and does not override abstract method CompareTo"?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Person implements Comparable<Person>


just add <Person> at the end of the class declaration line.

Or you can do:

change the compareTo(Person) to compareTo(Object)

public int compareTo(Object obj)
{

return this.happyIndex.compareTo(((Person)obj).happyIndex);
}
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problem with compareTo method. Please check the comparable interface method and java overriding.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The original error message did have a critical clue, that you omitted from your post:
Person is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable


You stopped at "compareTo" and overlooked the method argument: Object. Your implementation was for compareTo(Person), not compareTo(Object). That's the clue that led to Punit's answer above. You either need to tell the compiler that a Person is comparable to other Person, not objects - or you have to write a compareTo that can handle any Object, not just a Person.
 
Jayaram Subramanian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback and solving the issue
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic