Java - How to pass a Generic parameter as Class<T> to a constructor
- by Joe Almore
I have a problem here that still cannot solve, the thing is I have this abstract class:
public abstract class AbstractBean<T> {
private Class<T> entityClass;
public AbstractBean(Class<T> entityClass) {
this.entityClass = entityClass;
}...
Now I have another class that inherits this abstract:
@Stateless
@LocalBean
public class BasicUserBean<T extends BasicUser> extends AbstractBean<T> {
private Class<T> user;
public BasicUserBean() {
super(user); // Error: cannot reference user before supertype contructor has been called.
}
My question is how can I make this to work?, I am trying to make the class BasicUserBean inheritable, so if I have class PersonBean which inherits BasicUserBean then I could set in the Generic the entity Person which also inherits the entity BasicUser. And it will end up being:
@Stateless
@LocalBean
public class PersonBean extends BasicUserBean<Person> {
public PersonBean() {
super(Person.class);
}
...
I just want to inherit the basic functionality from BasicUserBean to all descendants, so I do not have to repeat the same code among all descendants. Thanks!.