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
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?
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 ]
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
posted
0
Hi,
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.