>>9
What
>>10 said. Inheritance means "child inherits from parent." The child can mask itself as the parent, e.g,
Piece playa = new Player(); - very useful for accessing a data structure containing multiple types objects inherited from a same base class), but you can't take a Piece and promote it into a Player with casting.
You can do this, if you want:
//EXAMPLE
public static Piece inputPiece(boolean isPlayer)
{
if(isPlayer) return new Player();
return new Treasure();
}
/* ... */
Piece thing = inputPiece( ... );
/* Player thing = (Player)inputPiece( ... ); */
/* Treasure thing = (Treasure)inputPiece( ... ); */
Of course, if you do one and inputPiece tries to return a Treasure object, it will fail at runtime. Over-all, unless you can guarantee that you already know what inputPiece will return on any given input, you should not do this.