At first look, I thought a value with type A with T can not be a
subtype of B
There's are two misconceptions here. First, that the static type of an instance has any bearing on the result of isInstanceOf
: there is none. To be clear, when doing a.isInstanceOf[B]
, the fact that a
is of type A with T
is not relevant.
The method isInstanceOf
is implemented at the bytecode level by the JVM. It looks at the class information every instance carries, and checks whether B
one of the classes (the class of the instance itself and its ancestors), or one of the implemented interfaces. That's the "is-a" relationship: "a is a B".
Technically, isInstanceOf
is part of Java's reflection, where it is known as instanceof
.
The second misconception is the inheritance can somehow remove a parent type. That never happens: inheritance only adds types, never removes them. The type A with T
is an A
, a B
, a T
, an AnyVal
and an Any
. So even if isInstanceOf
did look at the type A with T
, it would still return true.