简体   繁体   中英

Setting the type of a field in a superclass from a subclass (Java)

I am writing a project on Google App Engine, within it I have a number of abstract classes that I hope I will be able to use in my future projects, and a number of concrete classes inheriting from them. Among other abstract classes I have an abstract servlet that does user management, and I hava an abstract user. The AbstractUser has all the necessary fields and methods for storing it in the datastore and telling whether the user is registered with my service or not. It does not implement any project specific functionality. The abstract servlet that manages users, refers only to the methods declared in the AbstractUser class, which allows it to generate links for logging in, logging out and registering (for unregistered users). In order to implement the project-specific user functionality I need to subclass the Abstract user. The servlets I use in my project are all indirect descendants from that abstract user management servlet, and the user is a protected field in it, so the descendant servlets can use it as their own field. However, whenever i want to access any project specific method of the concrete user, i need to cast it to that type. ie

(abstract user managing servlet)
...
AbstractUser user = getUser();
...
abstract protected AbstractUser getUser();


(project-specific abstract servlet)
@Override
protected AbstractUser getUser() {
   return MyUserFactory.getUser();
}

any other project specific servlet:

int a = ((ConcreteUser) user).getA();

Well, what i'd like to do is to somehow make the type of “user” in the superclass depend on something in the project-specific abstract class. Is it at all possible?

And i don't want to move all the user-management stuff into a project-specific layer, for i would like to have it for my future projects already written :)

Thank you for your help.

Make your AbstractServlet generic:

public class AbstractServlet<T extends AbstractUser> { 
    private T user;
    protected abstract T getUser();
    ...

Then you can make the implementations use a specific subclass of AbstractUser :

public class ConcreteServlet extends AbstractServlet<ConcreteUser> { ...

Then you will no longer need to cast, as any place you have T in the base class, your implementation will now refer to ConcreteUser :

@Override
protected abstract ConcreteUser getUser() { .. }

I think what you need is generic class:

class AbstractUserManager<UserType extends AbstractUser> {
    protected UserType user;    

    public UserType getUser() {
        return user;
    }

    public void addUser(UserType u) {
        this.user = u; 
    }

    ...some other methods that use AbstractUser and dont need specific functionality...
}

//Class used for managing AbstractUsers with no extra features
class SimpleUserManager extends AbstractUserManager<AbstractUser> {
//No body as AbstractUserManager is enough
}

//Class that manages CrazyUsers
class CrazyUserManager extends AbstractUserManager<CrazyUser> {
    public CrazyUserManager() {
        this.setUser(new CrazyUser());
    }

    public someNewFunction() {
        this.getUser().someCrazyActionOnlyCrazyUsersHave();
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM