Newbie question here. I'm trying to teach myself, and sometimes the solo route can be frustrating. I'm working a problem in Liang's book Introduction to Java Programming 8th edition (Exercise 14.1) where I have to modify a GeometricObject class to implement Comparable and then define a static "max" method to find the larger of two GeometricObjects. Then I have to write a test program that uses this max method to find the larger of two rectangles. Previously in the book, the author has had us create a Rectangle subclass that extends GeometricObject. I've modified the GeometricObject class to implement Comparable, and I've created the max method. Then I've modified the Rectangle subclass to also implement Comparable. I wrote the test program as well.
But Eclipse is giving me the Big Red X by my Rectangle class's declaration saying "The interface Comparable cannot be implemented more than once with different arguments: Comparable and Comparable." That's when my class declaration looks like this:
public class Rectangle extends GeometricObject implements Comparable<Rectangle> {
I've tried removing the parameter altogether:
public class Rectangle extends GeometricObject implements Comparable {
... and the error message changes to "Comparable is a raw type. References to generic type Comparable should be parameterized." Plus, it adds a second error in this case: "The type Rectangle must implement the inherited abstract method Comparable.compareTo(Object)"
When I look at examples of this kind of declaration, they always seem to use the class name as the parameter for Comparable in the declaration, an in fact, that's what the books say to do. So why is it saying I can't do that here?
And then what about the second error regarding the compareTo() method? Why is it only being pointed out when I remove the parameter from the declaration? Code for the Rectangle class follows (if I need to, I can post the GeometricObject superclass as well):
public class Rectangle extends GeometricObject implements Comparable<Rectangle> {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color, boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * (width + height);
}
/** Implement the compareTo method defined in Comparable */
public int compareTo(Rectangle o) {
if (getArea() > ((Rectangle)o).getArea())
return 1;
else if (getArea() < ((Rectangle)o).getArea())
return -1;
else
return 0;
}
}
Yes it's a bit tricky, and might be confusing.
When you declare GeometricObject to be Comparable that mean that every GeometricObject can be compared with another GeometricObject- so you can sort list of GeometricObject (probably it will be it's sub-classes instances cause it's probably gonna be abstract). So when you implement Comparable in GeometricObject you need to implement this function:
public int compareTo(GeometricObject o) {}
in that point, when extending GeometricObject with Rectangle- Rectangle automatic deriving it's super class compareTo method. and when implement Comparable in Rectangle you need to implement this function:
public int compareTo(Rectangle o) {}
It's seems like there is no problem, cause it's seems like a regular overloading, but here is the catch- Generics in Java it's just a syntactic sugar for compiler- in run time all generic become Object so after compilation Rectangle going to contains 2 methods that will be the same.
So you have choose only one, although if we take your use case it's probably ok- you can implement only Comparable in super class and in Rectangle override it and check whether it's instance of Rectangle and do your specific logic or use the super class logic.