When you want an overrided method to take as argument a subclass of the argument of the overridden method, you usualy accomplish that with generics (as discussed in Template pattern with varying input type in overridden method). An example of that can be seen in my methodA
of the class Parent
and Child
I am wondering if it is possible to do the same thing with a method that takes as argument an object of the class itself (my methodB
)
abstract class Super {}
class Sub extends Super {}
abstract class Parent<S extends Super> {
abstract void methodA(S argument);
abstract void methodB(Parent<S> parent);
}
public class Child extends Parent<Sub> {
@Override
void methodA(Sub argument) {}
@Override
void methodB(Child child) {}
}
In the code above methodB
in Child
does not override methodB
from Parent
.
I could of course use casting in methodB
from Child
as in:
public class Child extends Parent<Sub> {
@Override
void methodB(Parent<Sub> child) {
(Child) child
}
}
But as a general rule I try to avoid casting as much as possible.
Is it possible to have an overrided methodB
in Child
(with signature @Override void methodB(Child child)
)? If so, what would the signature of methodB
(in Parent
) look like?