This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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
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
Joined: Apr 28, 2004
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...