| Author |
Implementing Comparable: "must implement the inherited abstract method" error
|
Charles Greenewald
Greenhorn
Joined: Jul 16, 2010
Posts: 6
|
|
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
Joined: Mar 09, 2009
Posts: 600
|
|
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
Joined: Jul 16, 2010
Posts: 6
|
|
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
Joined: Mar 09, 2009
Posts: 600
|
|
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
Joined: Jul 16, 2010
Posts: 6
|
|
So would it be something like this? Because that error message I quoted still isn't going away...
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Charles:
Does your definition of Shape look like this?:
John.
|
 |
Charles Greenewald
Greenhorn
Joined: Jul 16, 2010
Posts: 6
|
|
Like this:
What's the <Shape> part in yours?
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
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
Joined: Jul 16, 2010
Posts: 6
|
|
Bingo! Thank you! I guess after I turn in this homework, I'll study what generics means.
Thanks again,
Charles
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Charles:
This link is a good resource for generics.
John.
|
 |
Charles Greenewald
Greenhorn
Joined: Jul 16, 2010
Posts: 6
|
|
Thank you very much. I'll study it.
Charles
|
 |
 |
|
|
subject: Implementing Comparable: "must implement the inherited abstract method" error
|
|
|