Я должен вернуть один из реализующих классов в качестве типа возврата интерфейса.
public class GenericReturnType {
public static <T extends Interface2> T resolveClazz(boolean isI3, Class<T>i3Clazz, Class<T>i4Clazz) throws IllegalAccessException, InstantiationException {
if(isI3) {
return i3Clazz.newInstance();
}
else {
return i4Clazz.newInstance();
}
}
}
Но когда я вызываю этот метод как
GenericReturnType.resolveClazz(true, Interface3Impl.class, Interface4Impl.class);
Я получаю ошибку времени компиляции как
error: method resolveClazz in class GenericReturnType cannot be applied to given types;
Interface3 i3 = GenericReturnType.resolveClazz(true, Interface3Impl.class, Interface4Impl.class);
^
required: boolean,Class<T>,Class<T>
found: boolean,Class<Interface3Impl>,Class<Interface4Impl>
reason: inferred type does not conform to equality constraint(s)
inferred: Interface4Impl
equality constraints(s): Interface4Impl,Interface3Impl
where T is a type-variable:
T extends Interface2 declared in method <T>resolveClazz(boolean,Class<T>,Class<T>)
Вот структура интерфейсов, которые у меня есть. Структуры интерфейса
Может кто-нибудь, пожалуйста, помогите мне с тем, что я делаю здесь неправильно