This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "comparable Interface" Watch "comparable Interface" New topic
Author

comparable Interface

Jayaram Subramanian
Greenhorn

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);
}


SCJP 6
Thillakan Saba
Ranch Hand

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


SCBCD, SCJP & MCP
HowToAskQuestions
Mike Simmons
Ranch Hand

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

Joined: Aug 12, 2008
Posts: 7
Thanks for your feedback and solving the issue
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: comparable Interface
 
Similar Threads
compareTo() method
compareTo() doubt
Error in K&B Study Guide?
why the output is classcastexception ?
Collections