I have generic abstract superclass GenericModel with method that I would like to call in children classes as follow:
public abstract class GenericModel<T extends GenericModel> {
@Transient protected Class<T> entityClass;
public GenericModel() {
Class obtainedClass = getClass();
Type genericSuperclass = null;
for(;;) {
genericSuperclass = obtainedClass.getGenericSuperclass();
if(genericSuperclass instanceof ParameterizedType) {
break;
}
obtainedClass = obtainedClass.getSuperclass();
}
ParameterizedType genericSuperclass_ = (ParameterizedType) genericSuperclass;
entityClass = ((Class) ((Class) genericSuperclass_.getActualTypeArguments()[0]));
// some code
public List<T> getByCreationUser_Id(Long id) {
// method body
return entities;
}
}
I am extending class this class with:
public class Insurance extends GenericModel<Insurance> {
// some code
}
And I wanna extend class: Insurance
with this class as follow:
public class Liability extends Insurance {
}
and object of Liability
class I would like call method: getByCreationUser_Id()
and instead of list of TI would like to get List<Liability>
as follow:
List<Liability> currentUserInsurances = new Liability().getByCreationUser_Id(1L);
Unfortunatelly I am getting an error:
Type mismatch: cannot convert from List<Insurance> to List<Liability>
When I will call same method on Insurance class it works, I am getting an: List<Insurance>
I've already tried declare classes as follow:
public class Insurance<T> extends GenericModel<Insurance<T>> {}
public class Liability extends Insurance<Liability> {}
But id doesn't work. Compile errors occur.
Please help.
You need to declare your Insurance
class as,
public class Insurance<T extends Insurance> extends GenericModel<T> {}
And then, your Liability
class as,
public class Liability extends Insurance<Liability> {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.