Originally Posted by
Darryl.Burke
Apart from all that, if you need to know which subclass's method is invoked, it's almost always a sign of misuse of inheritance.
db
^^ this is very important to keep in mind, as this is the basis for creating well-formed polymorphic classes, as well as properly using inheritance.
If you want a way to differentiate the name of different players, put a string field in your base class.
public abstract class Player
{
public final String name;
public Player(String name)
{
this.name = name;
}
}
public class Orc extends Player
{
public Orc()
{
super("orc");
}
}
public static void main(String[] args)
{
Player p1 = new Orc();
Player p2 = new Wizzard(); // code for Wizzard not provided, but it's something similar to Orc
System.out.println(p1.name);
System.out.println(p2.name);
}
If you have a set list of names possible, simply use an enumeration instead of a string.