Superclass to subclass (I think)

So I have a lobby where I hold a list of the type Player.
Now when the player decides to play a game it can choose a class such as Heavy, Sniper and Assault.
The classes Heavy, Sniper and Assault are subclasses of the type Player.
So a logical thing would be to do upon pressing the play button “player = new Heavy();”
This however does not give me the attributes I had set in the player object.
For example:
I call the function player.setRoom(someroom);
Then the player chooses the Heavy class.
player = new Heavy();
Now I call player.getRoom().update/render ();
but player.getRoom() is now null.

I don’t want to do this in the Heavy constructor:
Heavy(Player player) {
room = player.getRoom();
}
I’m looking for an OO solution

when you call

player = new Heavy();

you are throwing away the old player and all of it’s data, so the newly constructed player is not going to have a room set.

Two options would be to have:

player = new Heavy(player.getRoom());

and set the room in the constructor.

Or, you can just not create the player object at all until a class has been selected and then set the room.

The 1st solution is what I want to avoid, it would be my last choice if any other.
I need to store some fields in the player object so the 2nd solution won’t work.

Ok, there are three more options I can think of then.

First, create a PlayerData class that contains the data for a player that you need to store before you create it, then call
player = new Heavy(PlayerData);
to instantiate the object.

Second, not have subclasses for each of the different classes and rather just have a variable that can either be HEAVY, SNIPER, or ASSAULT. Then when you know what class it will be call
player.clazz = HEAVY;

Third, if you feel like you need there to be subclasses, you could set up a sort of factory for the player, then call
PlayerFactory.createHeavy(allTheDataYouNeed);
In there you can construct a heavy using all the data, however this is very similar to my first idea. Up to preference really

Differantiate between the player and the game character the player chooses.
Create a player object and assign the chosen character later.
Neither inherits player the character nor the character the player.

I agree with @65K. The Player you describe is more like a Session. This Session might include your Profile and the room you joined.
The Heavy, Assault and Sniper instead are part of the game itself. The should probably have a shape used for collision detection, a weapon and i guess they should react to user input.
If you want to show the name in the chat or whereever, you can simply use the Session and use it’s name.

Why are the different player types subclasses of Player?

I would just make that an attribute of the Player class. Something like this:

public class Player{
   enum PlayerClass{
      HEAVY, SNIPER, ASSAULT, KITTEN;
   }

   PlayerClass myClass;

   public void setPlayerClass(PlayerClass pc){
      myClass = pc;
      //maybe set other attributes that depend on class?
   }
}

In other words, favor composition over inheritance.

Ah this clears my mind up a bit. I was told to use inheritance when I posted a thread about this.
It’s all clear now, I agree the Player is more like a Session :slight_smile:
Thanks for the help