File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help on Comparable 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 "help on Comparable" Watch "help on Comparable" New topic

help on Comparable

Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42

I have tried to implement Comparable interface and the compareTo method of it for my own class Name. But the compiler says that as Name is not defined as abstract it cannot implement abstract method compareTo. I give the code I tried as follows:

Can anyone please clarify it?

if you think you can you r right<br />if you think you can not you r double right
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
Whats the Name pameter that you are giving to compareTo mathod
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

with Neme as parameter it is not a regular implementation of the compareTo method.

[ September 23, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Hello all,

I tried the code resembles the one given in Head First Java.
The program tries to sort a name list file contains first and last names. The comparision is based on first name.

The compareTo method works fine with Strings. But to compare the custom class we need to implement Comparable interface and override compareTo method. Am I right?
Krishnamoorthy Sethuraman

Joined: Dec 12, 2005
Posts: 24
Change the class declaration as:
class Name implements Comparable<Name>
and handle the exception in the go() method. Code will compile.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Here is the error message: "Name is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable".

It tells you exactly what is wrong. As other posters (Naveen and Burkhard) have written above, the signature of your compareTo method is incorrect. You have overloaded, not overridden, the compareTo method.
[ September 23, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
One other thing, after fixing the code don't expect the 'Name' class' compareTo to be called, because in the 'getNames()' method 'String' is being added to the list and not 'Name' type, therefore Collections.sort(namelist) will make use of the String object's compareTo().

If you want to test your compareTo(), you must add Name type to the list as below.
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42

thanks for your valuable comments. I identified what mistake I have comitted. I corrected them and my code worked well. Thanks a lot especially to Krishnan...
I agree. Here's the link:
subject: help on Comparable
It's not a secret anymore!