I have an abstract class called user, and 2 sub classes: RegisteredUser & VisitorUser. I have a need to convert a VisitorUser object to a RegisteredUser object – do I use casting to achieve this? If so, how?
I have an abstract class called user, and 2 sub classes: RegisteredUser & VisitorUser.
Share
You can’t. You can view either as a
Userbut you cannot castRegisteredUsertoVisitorUseror vice versa, as they are different types.You could make a conversion method, that returns a new instance of
RegisteredUserfrom an instance ofVisitorUser. However, I would rethink the abstraction so the status of the user becomes a part of the state of the object instead of a part of the type itself.