public class Driver { public static void main(String [] args) { Person regPerson = new Person(); Hero supPerson = new Hero(); Dog rover = new Dog(); // Instanceof checks if the object is a certain type or // a subclass of that type (e.g., a Hero is a Person) if (regPerson instanceof Person) System.out.println("regPerson is a type of Person"); if (supPerson instanceof Person) System.out.println("supPerson is also a type of Person"); // Checks for non-hierarchical: Compiler prevents non-sensical checks //if (rover instanceof Person) // System.out.println("[Should never appear]: rover is also a " + // "type of Person"); if (supPerson instanceof Hero) System.out.println("supPerson is a type of Hero"); // Checks within hierarchy: Compiler doesn't prevent if (regPerson instanceof Hero) System.out.println("[Should never appear]: regPerson is a type "+ "of Hero"); } }