In the below code why i am getting error while doing downcasting?

//overwritten parameters
class birds;
int size_inch = 20;
int lifetime = 10;
string colour = “brown”;

function void get_properties;
$display (size_inch, lifetime, colour);
endfunction
endclass

class parrot extends birds;
string colour = “green”;
function void get_properties();
$display (size_inch, lifetime, colour);
endfunction
endclass

module a;
birds b;
parrot p;
initial begin
b = new();
p = b;
$cast (p,b);
p.get_properties;
b.get_properties;
end
endmodule

In reply to Sujatha.T:

Hi Sujatha,
Please, use code tag it will help to understand your code easy way.



module a;
  birds b;
  parrot p,p1;
  initial begin
    /*
    b = new();
    p = b; // it is illegal to assign base handle to derived class. it will give error
    $cast (p,b);
    */
    p = new();
    b = p;        
    $cast (p1,b); // Down cast is only possible if base class handle pointing to derived class handle.
    p.get_properties;
    b.get_properties;
    p1.get_properties;
  end
endmodule


Please check below paper. Downcast is well explained in this.
http://www.sunburst-design.com/papers/CummingsSNUG2018SV_Virtuals.pdf

Thanks!