Java - How to pass a Generic parameter as Class<T> to a constructor

Posted by Joe Almore on Stack Overflow See other posts from Stack Overflow or by Joe Almore
Published on 2011-11-11T17:30:14Z Indexed on 2011/11/11 17:52 UTC
Read the original article Hit count: 154

Filed under:
|
|
|

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!.

© Stack Overflow or respective owner

Related posts about java

Related posts about generics