It seems like you usually implemented the java.lang.Comparable
interface without specifying the type parameter.
public abstract class Area implements Comparable {
@Override
public int compareTo(Object other) {
if (other instanceof Area)
return new Double(getArea()).compareTo(other.getArea());
return -1; // or something else
}
abstract public double getArea();
}
Since I only want to compare apples with apples, I think it would make sense to specify the type.
public abstract class Area implements Comparable<Area> {
@Override
public int compareTo(Area other) {
// ...
If I want to introduce another class to compare Area
with, I thought I could do the following:
public abstract class Area implements Comparable<Area>, Comparable<Volume> {
@Override
public int compareTo(Area other) {
// ...
}
@Override
public int compareTo(Volume other) {
// ...
}
}
But the java compiler tells me:
Area.java:2: error: repeated interface
public abstract class Area implements Comparable<Area>, Comparable<Volume> {
^
Area.java:2: error: Comparable cannot be inherited with different arguments: <Area> and <Volume>
- Are there any drawbacks specifying the type argument for the generic interface?
- Why won't Java allow me this multiple implementation?
Note: I'm using Java version 1.7.0_45