0

I need to mapped a model object, which extends into the object to be inizialized.

Example:

class User{
}
class RequestUser extends User{
}

void main(){
    User user = new User();
    RequestUser reqUser = user;
}

Is there a way to do it native ?, or should I use an external library? Which do you recommend?

Thanks!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

Take a look in this link -> explicit casting from super class to subclass.

If you instantiate a new UserRequest, it will have UserRequest and User properties, UserRequest extends User. Maybe you should do this:

class User{
    int id;
    String name;
}
class RequestUser extends User{
    int requestCode;
}

public void teste(){
    User user = new RequestUser() {{
        id = 0;
        name = "User1";
    }};
    RequestUser reqUser = (RequestUser)user;
}
0

Why "RequestUser" must be a class? It should be a instance of User.