This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract compareTo() errors

 
Theresa Marlin
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!!! I think I have figured out the problem....
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(For the sake of completeness, and for those that might read this thread after us, what did you do?)
 
Theresa Marlin
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic