aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance, how to "upgrade" an instance" Watch "Inheritance, how to "upgrade" an instance" New topic
Author

Inheritance, how to "upgrade" an instance

Deborah Larsson
Greenhorn

Joined: Oct 30, 2009
Posts: 10
Hello,

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
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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
Greenhorn

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
Greenhorn

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance, how to "upgrade" an instance
 
Similar Threads
Getting rid of unchecked operations related to Vector class
calling core java program ffrom servlet
scope of objects
OO design question?
Sorting an ArrayList is giving me fits