Dynamic cast - Classes

The following is from the reference manual:

When $cast is applied to class handles, it succeeds in only three cases:

  1. The source expression and the destination type are assignment compatible, that is, the destination is
    the same type or a superclass of the source expression.
  2. The type of the source expression is cast compatible with the destination type, that is, either:
    — the type of the source expression is a superclass of the destination type, or
    — the type of the source expression is an interface class (see 8.26)
    and the source is an object that is assignment compatible with the destination type. This type of
    assignment requires a run-time check as provided by $cast.
  3. The source expression is the literal constant null.

I don’t understand the line after “and”, first it says the for it needs to be cast compatible which means source expression is of the superclass type and then after “and” it says the expressions must be assignment compatible. How can it be both?

In reply to acpatel5:

There’s a difference between the types of the class variables versus the types of the class objects contained by the variables.

class A;
endclass
class B extends A;
endclass
A a1=new, a2;
B b1=new, b2;

Then if you have

$cast(b2,a2);

That satisfies the part before the and. But if the statement before it was a2=a2;, the object handle contained in a2 is not assignment compatible with b1, so it would fail.

In reply to dave_59:

Can you elaborate a bit on the working example and say how it satisfies both(cast compatible and assignment compatible) the conditions?