File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes abstract compareTo() errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract compareTo() errors" Watch "abstract compareTo() errors" New topic
Author

abstract compareTo() errors

Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
I am writing a program that has the compile error: Error: Vehicle is not abstract and does not override abstract method compareTo(Vehicle) in java.lang.Comparable
I'm not sure why I'm getting this, because I don't even have any abstract methods. I am fairly new to the Comparable interface, and hardly understand it. Thanks in advance for your help!


A tester class:


Main:

Thanks again!!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, the error message is providing you pretty accurate information. You're defining the class as implementing Comparable<Vehicle>... but your compareTo method is taking an Object. So when the compiler says does not override abstract method compareTo(Vehicle) it's correct.
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
So I need to get rid of the Object obj and somehow implement comparable there? Or do I get rid of "implements comparable<vehicle>" ?

Thanks!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Theresa Marlin wrote:So I need to get rid of the Object obj and somehow implement comparable there? Or do I get rid of "implements comparable<vehicle>" ?

Thanks!


Part of the excitement and fun of programming is to play around with your code, experiment with it, and see what happens.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I guess I'd just echo what Pete says, and quote part of my original response:
David Newton wrote:... when the compiler says does not override abstract method compareTo(Vehicle) it's correct.

My guess is that if you reply with what you think that error message might mean you'll probably do a forehead smack.
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
Thank you for your help!!! I think I have figured out the problem....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(For the sake of completeness, and for those that might read this thread after us, what did you do?)
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
Well, first I attempted to put abstracts in front of classes and methods to see if that would get rid of the error, but it didn't. After a fair amount of playing around, I fixed the compareTo() method and changed it to the following:



After this, and a slight unrelated modification to the toString() method, the program worked.

Thanks again for everyone's help!!!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Cool, looks good. (Although I'd name the parameter "v" or "vehicle"... "veh" is disturbingly close to "meh", and we wouldn't want the poor vehicle to think we were indifferent to it :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract compareTo() errors
 
Similar Threads
Problem with setProperty
Getting grades into a gradebook
Sorting an array of objects
abstract class
Storing objects polymorphically in a Figure array