I would like to override a generic function in a subclass as follows:
superclass:
public abstract class Metric {
public abstract <T extends Foo> T precompute();
public abstract <T extends Foo> void distance(T arg);
public static class Foo {}
}
Subclass:
public class MetricDefault extends Metric {
@Override
public Bar precompute() { return new Bar(); }
@Override
public void distance(Bar arg) {}
public static class Bar extends Metric.Foo {}
}
A generic function, when the generic type is the return value of a function, is valid Java code and succeeds.
Changing only the placement of the generic type - turning it into an argument to the function, not the return type - and this becomes invalid Java code.
Why is the last case invalid? What can I do to implement this feature?
source
share