I have the following static method:
class Foo {
public static <T> List<T> populate(ResultSet rs, Class<? extends T> klass) {
// ...
}
// ...
}
And I have
class MyConcreteClass extends MyAbstractBaseClass {
// ...
}
When I try to call the abstract method like this:
List<MyAbstractBaseClass> result = Foo.populate(rs, MyConcreteClass.class)
… then I get the following compilation error:
Compilation failure
List<MyConcreteClass> cannot be converted to List<MyAbstractBaseClass>
However, if I call it like this:
List<MyAbstractBaseClass> result = Foo.<MyAbstractBaseClass>populate(rs, MyConcreteClass.class)
… then the compiler seems satisfied.
My question:
- Why does Java require an explicit type parameter in the above example?
- Given that my result must be a
List<MyAbstractBaseClass>
, is there any way I can specify the signature of my static method differently, such that I don’t have to provide the type parameter, when I call it?
Edit: Specified that the result type is a given, and cannot be changed.