I am trying to get the following logic to work:
public class Wrapper {
public static class General {
public void copy(General g) {
// copy fields
}
// General contents.
}
public static class Specific extends General {
// Specific contents.
}
public static class GeneralLogic<T extends General> {
public T load(ResultSet rs) {
General o = new General();
// Do stuff
return new General();
}
}
public static class SpecificLogic<T extends Specific> extends GeneralLogic<T> {
@Override
public T load(ResultSet rs) {
Specific o = new Specific();
o.copy(super.load(rs));
// Do stuff
return new Specific();
}
}
}
Both return lines result in a compile error: Type mismatch: cannot convert from Wrapper.General to T (Wrapper.Specific to T for the second return).
If
Tis a subtype ofGeneral, an instance ofGeneralisn’t necessarily aT. If you want to return aT,you’ll have to get it from another factory method (as an argument or abstract method), or by accepting aClass<? extends T>and instantiate it via reflection.