This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inheritance, how to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance, how to "upgrade" an instance" Watch "Inheritance, how to "upgrade" an instance" New topic

Inheritance, how to "upgrade" an instance

Deborah Larsson

Joined: Oct 30, 2009
Posts: 10

I am working on a networked game where I have (amongst others ) a Player and a GameHost class.
The GameHost extends the Player class.
Now when the function Player.startnewgame() is done I want the player to become a Gamehost instance. I seem to have lost the insight in how this works.
I can create a new GameHost() but that does not convert the current instance, right? Doesn't the player still exist?
Is the best to

I hope someone can help clarify the idea.
thanks ahead

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44581
That doesn't sound possible. Once you create an instance it arises from a particular class, and you can't change the class it arises from.

A few vague ideas, however:

You could replace it with an instance of the new class, particularly if you give it a copy constructor. But maybe "favour composition over inheritance" will come to your aid. Create a wrapper class with a Player instance as a field. When you call the "begin" method, you can replace the Player field with one of its subclasses.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11882

if you create a Player like this

Player currentPlayer = new Player();

then that will forever and always be a Player. but you might be able to do this:

The currentHost reference can change to whichever you need it to...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Deborah Larsson

Joined: Oct 30, 2009
Posts: 10
Thank you for the explanation and help.
I will simply change the code then to create the new Player or object or new GameHost depending on selecting a gamesession or creating one.
On the selection screen it is thus neither but I guess it is not necessary either.
Deborah Larsson

Joined: Oct 30, 2009
Posts: 10
Sorry, it seems it is not solved.

I can create the Player object when the player choses to join an existing game or create a GameHost when creating a new game.
However after the game is done everyone returns to the loginscreen where one can choose to either again become a host or player.
How should I handle this, since I cannot change the type that they are? Discard those existing instances when they create a new game or join one?

The reason why I have a gamehost subclass of player is since it has extra methods like start and stopgame. Maybe this is a design flaw?

thanks ahead for help.

jQuery in Action, 3rd edition
subject: Inheritance, how to "upgrade" an instance
It's not a secret anymore!