From the comments A : D
, so all A
are D
, but not all D
are necessarily A
.
So with two variables:
A classA;
D classD = ...
We need
classA = (A) classD;
This does a type-check, to confirm that the specific classD
is actually also an A
. If this is the case (or it is null
) then the assignment is successful; otherwise an exception occurs.
This is to prevent you assigning something to classA
that is actually not really an A
.
Note it is implicit the other way, since the compiler knows it to be valid:
classD = classA; // always valid - no type check