File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help on Comparable

 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?
 
Naveen Zed
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the Name pameter that you are giving to compareTo mathod
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

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


Bu.
[ September 23, 2006: Message edited by: Burkhard Hassel ]
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic