Consider the above code edited
class Color {}
class Red extends Color {}
class Blue extends Color {}
class A {
public static void main (String[] args)
{
Color color1 = new Red();
Red color2 = new Red();
boolean b1 = color1 instanceof Color;
boolean b4 = color1 instanceof Blue;
boolean b2 = color2 instanceof Blue;
System.out.print(b1+","+b2+","+b4);
}}
I think only at runtime the object refered is considered, at compile time the reference is considered.
color1 is of type Color, it can get both red or blue object, so we cant decide what is the object is passed at compile time.
color2 is of type Red, it can hold only red and not blue, so compile error is got implys that color2 cant be an instanceof blue at all
Hence at compile time, object's reference hierarchy is considered
and at run time, object hierarchy is considered