class Animal
{
}
class Dog extends Animal
{
}
class main
{
public static void main(String args[])
Animal g= new Dog();
System.out.println(g instanceof Dog); // 1st case
System.out.println(g instanceof Animal); // 2nd case
}
QUESTION: why the output is true in both cases ?
Because the object that is referenced, at run-time, by local variable
gis of typeDog(and thus also anAnimal, becauseDog extends Animal, though that’s missing from your example).