Object Typecasting Example

Contact Us or call 1-877-932-8228
Object Typecasting Example

Object Typecasting Example

Say our game program needed to store references to Entity objects.

  • The exact type of Entity would be unknown at compile time.
  • But during execution we would like to instantiate different types of Entity at random.

Perhaps our code for Entity includes a method moveTo() that moves it to a new location. Many of the entities move the same way, but perhaps some can fly, etc. We could write a generally useful form of moveTo in the Entity class, but then override it as necessary in some of the classes derived from Entity.

Code Sample:

Java-Inheritance/Demos/EntityTest.java
class Entity {
  private String name;
  public Entity(String name) { this.name = name; }
  public String getName() { return name; }
  public void move() {
    System.out.println("I am " + name + ". Here I go!");
  }
}

class Playable extends Entity {
  public Playable(String name) { super(name); }
  public void move() {
    System.out.println("I am Playable " + getName() + ". Here we go!");
  }
}

class Ogre extends Entity {
  public Ogre(String name) { super(name); }
}

class Troll extends Entity {
  public Troll(String name) { super(name); }
}

class Prince extends Playable {
  public Prince(String name) { super(name); }
  // does not override public void move()
}

class Princess extends Playable {
  public Princess(String name) { super(name); }
  public void move() {
    System.out.println("I am Princess " + getName() + 
        ". Watch as I and my court move!");
  }
}

class Wizard extends Playable {
  public Wizard(String name) { super(name); }
  public void move() {
    System.out.println("I am the Wizard " + getName() + 
        ". Watch me translocate!");
  }
}

public class EntityTest {
  public static void main(String[] args) {
    String[] names = { "Glogg", "Blort", "Gruff", 
                      "Gwendolyne", "Snow White", "Diana",
                      "Merlin", "Houdini", "Charles", "George" };
    for (int i = 0; i < 10; i++) {
      int r = (int) (Math.random() * 5);
      Entity e = null;
      switch (r) {
        case 0: e = new Ogre(names[i]); break;
        case 1: e = new Troll(names[i]); break;
        case 2: e = new Wizard(names[i]); break;
        case 3: e = new Prince(names[i]); break;
        case 4: e = new Princess(names[i]); break;
      }
      e.move();
    }
  }
}

The compiler allows the calls to the moveTo method because it is guaranteed to be present in any of the subclasses - since it was created in the base class.

  • If not overridden in the derived class, then the base class version will be used.
  • If the method is overridden by the derived class, then the derived class version will be used.

At runtime, the JVM searches for the method implementation, starting at the actual class of the instance, and moving up the inheritance hierarchy until it finds where the method was implemented, so the most derived version will run.

Next