Dynamic cast failed

Hi all,why i got error? Thanks

class base;
  int a = 5;
endclass

class extend extends base;
  int b = 1;
endclass

module top;
  initial begin
    base     base;
    extend  extend;
    
    extend = new();
    base    = new();
    $cast(extend, base);
    $display(extend.a);
  end
endmodule

In reply to peter:

You cannot cast a base class variable to an extended class variable when the base class variable does not contain a type compatible object. extend.b would not exist. Please review the link I gave you in your previous question.

In reply to dave_59:

In reply to peter:
You cannot cast a base class variable to an extended class variable when the base class variable does not contain a type compatible object. extend.b would not exist. Please review the link I gave you in your previous question.

Thanks for reply.
if base_handle is a reference to derived class, and i use the $cast(extend_handle,base_handle), it is permitted. if base_handle is not a reference to derived class , it is not permitted using $cast(extend_handle,base_handle).
is it correct?

In reply to peter:

That is correct for your example. I strongly suggest you see my SystemVerilog OOP course on classes for more details on up-casting versus down-casting and how that relates to inheritance.

In reply to dave_59:

In reply to peter:
That is correct for your example. I strongly suggest you see my SystemVerilog OOP course on classes for more details on up-casting versus down-casting and how that relates to inheritance.

Thanks a lot!