Identifying Objects : instanceof « Operators « Java Tutorial






class Animal {
  public String toString() {
    return "This is an animal ";
  }
}
class Dog extends Animal {
  public void sound() {
    System.out.println("Woof Woof");
  }
}
public class MainClass {
  public static void main(String[] a) {
    Dog aDog = new Dog();
    if (aDog instanceof Animal) {
      Animal ani = (Animal) aDog;
      System.out.println(ani);
    }
  }
}
This is an animal








3.10.instanceof
3.10.1.The instanceof Keyword
3.10.2.Identifying Objects
3.10.3.Finding Out of what Class an Object is Instantiated
3.10.4.instanceof operator and class hierarchy