That's because you (probably) cannot cast from tabel[i]
to coloredRectangle
.
It's stated in the JLS that instanceof
will cause compilation problems if the above occurs:
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.