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(); // valid syntax
public abstract <T extends Foo> void distance(T arg); // valid syntax
public static class Foo {}
}
Subclass:
public class MetricDefault extends Metric {
@Override
public Bar precompute() { return new Bar(); } // Valid - return type extends Foo
@Override
public void distance(Bar arg) {} // Invalid ??? - argument type extends foo
public static class Bar extends Metric.Foo {}
}
The generic function, when the generic type is the return value of the function, is valid Java code and builds successfully.
Changing only the placement of the generic type – making it the argument to the function, rather than the return type – and it becomes invalid Java code.
Why is the latter case invalid? What can I do to implement this functionality?
then if as I understood Bar extends Foo: