For this problem pretend I am programming a forum. This has two kinds of users: normal users and administrators. Now suppose the forum needs more administrators in orden to optimize the management. However, the new administrators must be picked from the existing regular users. Administrator class inherits from User.
This is what I am struggling to achieve. I have tried both methods showed below but they do not work since the User argument is still a User once it leaves the method.
// First attemp trying casting
public void turnIntoAdminCasting(User u){
u = (Administrator) u;
}
// Second attemp with constructors
public void turnIntoAdminConstructor(User u){
u = new Administrator(u);
}
public static void main(String[] args){
User u = new User();
User u2 = new User();
turnIntoAdminCasting(u);
turnIntoAdminConstructor(u2);
// Both u and u2 are still User instances
}