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

Posted by Ibolit on Stack Overflow See other posts from Stack Overflow or by Ibolit
Published on 2010-06-18T11:13:47Z Indexed on 2010/06/18 11:33 UTC
Read the original article Hit count: 150

Filed under:
|
|

Hi. 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. i.e.

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

© Stack Overflow or respective owner

Related posts about java

Related posts about design-patterns