Regarding Method Overriding / Polymorphism SystemVerilog

In reply to dave_59:

So in the above code I have posted, Class A’s handle ‘a’ is created by ‘new’. Then I do a $cast(b,a). Why is there an error ‘type mismatch’?

But the following code works and why is that?


class A;
   int a=10;
   virtual function void disp();
     $display("Base a=%d",a);
   endfunction
endclass
 
class B extends A;
  int a=20; 
  function void disp();
    $display("Derived a=%d",a);
  endfunction
endclass
 
module tb();
  A a;
  B b=new;

  initial begin
    b=a;
    $cast(b,a);
 
    b.disp();               //
    $display("b.a=%d",b.a); // 
    a.disp();               //
    $display("a.a=%d",a.a); //  
  end
 
endmodule