Reputation: 2429
I have an abstract class named Individuo
that implements the Comparable<Individuo>
interface.
Yet when I try to override the method in the child classes it implements the following method:
@Override
public int compareTo(Object o) {
return 0;
}
Shouldn't it implement the following?
@Override
public int compareTo(Individuo o) {
return 0;
}
Here are the declarations
Individuo:
public abstract class Individuo<I extends Individuo> implements Cloneable, Comparable<Individuo>
Individuo_MultiOOP (child):
public class Individuo_MultiOOP extends Individuo implements Cloneable
Upvotes: 0
Views: 196
Reputation: 1000
Edit 2 (after question update)
Since Individuo is declared with generic type so use
Comparable<Individuo<I>>
instead of
Comparable<Individuo>
in declaration.
Original answer
First save the class with implementing declaration without overriding compareTo()
-
public class IndividuoChild extends Individuo {
...
}
Then from Compiler error note [can be found in IDEs like eclipse, intellij], select "Add unimplemented methods" which will generate expected compareTo()
as -
@Override
public int compareTo(Individuo o) {
// TODO Auto-generated method stub
return 0;
}
Upvotes: 1
Reputation: 2061
No, you should override with same argument. It will be more readability and easily to understand.
class Individuo implements Comparable<Individuo>{
@Override
public int compareTo(Individuo o) {
return 0;
}
}
Child class
public class Child extends Individuo{
@Override
public int compareTo(Individuo o) {
// TODO Auto-generated method stub
return 0;
}
}
If you use Object, the design maybe a problem.
Upvotes: 0
Reputation: 49656
It's likely that the children extend a raw version of Individuo
which is generalised by some arguments.
In this case, compareTo(Object o)
in the children will compile:
abstract class Individuo<T> implements Comparable<Individuo<T>> {}
class Child extends Individuo {
@Override
public int compareTo(Object o) {
return 0;
}
}
Make sure the children extend a correct Individuo
with all generic parameters provided:
class Child extends Individuo<Child> {
@Override
public int compareTo(Object o) { // this shouldn't compile
return 0;
}
}
Upvotes: 0
Reputation: 341
@Override
public int compareTo(Individuo o) {
return 0;
}
This is the method which needs to be overridden, if you have specified the generic type. I mean when your class signature is:
public class Individuo implements Comparable < Individuo >
But in cases you have not specified the generic type of comparable interface, it's OK to override public int compareTo(Object o) method. I mean in the case your class signature is:
public class Individuo implements Comparable
Let me know if your issue is resolved bro
Upvotes: 0