• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Comparable: "must implement the inherited abstract method" error  RSS feed

 
Charles Greenewald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a homework assignment which requires that I have an abstract super class called Shape, and multiple sub classes for specific shapes, in this case Rectangle. Shape implements Comparable, and I've written this method:



Now when I go to create Rectangle, which extends Shape, Eclipse tells me
The type Rectangle must implement the inherited abstract method Comparable.compareTo(Object)


How do I do that? Do I have to create a separate compareTo within Rectangle? And if I do, do I have to specify everything for it (rendering the Shape.compareTo useless...), or can I somehow use Shape.compareTo to perform the actions I need?

Thanks!
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles:

You might think about taking an alternate approach. Having compareTo() in Shape isn't really very useful, although you could create a compareArea() method to do what you're doing. Consider that two Triangle objects could have very different side lengths and angles, yet give you the same area. Having each subclass implement compareTo() allows you to tailor it to each shape.

John.
 
Charles Greenewald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, one of the criteria for the assignment is that the Shape class
Have a (non-abstract) compareTo method that compares Shapes based on their area.


I might end up just putting the same compareTo code in each sub class...would those automatically override the Shape.compareTo method?

Thank you for your answer. If I was the one making the decision, I would do it the way you suggested. That way I would be able to tailor each one to the specific shape it referred to...
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles:

You can call super.compareTo() in each subclass' compareTo() method, and use it to incorporate the area into the comparison.

John.
 
Charles Greenewald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would it be something like this? Because that error message I quoted still isn't going away...

 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles:

Does your definition of Shape look like this?:

John.
 
Charles Greenewald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this:



What's the <Shape> part in yours?
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles:

Are you using JDK 1.5 or above? Comparable uses generics, so your Shape class definition needs to look like mine. I created a sandbox project with a Shape and a Triangle class. With my Shape class defined as above, I'm not seeing the message you're getting.

John.
 
Charles Greenewald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo! Thank you! I guess after I turn in this homework, I'll study what generics means.

Thanks again,

Charles
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles:

This link is a good resource for generics.

John.
 
Charles Greenewald
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I'll study it.

Charles
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!