This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes comparable Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "comparable Interface" Watch "comparable Interface" New topic

comparable Interface

Jayaram Subramanian

Joined: Aug 12, 2008
Posts: 7

Why am i getting error "Person is not abstract and does not override abstract method CompareTo"?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: May 15, 2007
Posts: 53
problem with compareTo method. Please check the comparable interface method and java overriding.

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
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

Joined: Aug 12, 2008
Posts: 7
Thanks for your feedback and solving the issue
I agree. Here's the link:
subject: comparable Interface
It's not a secret anymore!