Constraints are Virtual in Nature
//constraints are virtual in natur
class eth_pkt;
rand bit [7:0] count;
constraint count_c {count < 10;}
endclass
class eth_ext_pkt extends eth_pkt;
constraint count_c {count > 50;}
endclass
module top;
eth_pkt pkt =new();
eth_ext_pkt epkt =new();
initial begin
assert(pkt.randomize());
$display("pkt.count = %0d <= here the pkt is less than 10\n",pkt.count);
assert(epkt.randomize());
$display("epkt.count = %0d\n",epkt.count);
$display("before casting",$typename(pkt));
$display("pkt type is %s\n",$typename(pkt));
$cast(pkt,epkt);
assert(pkt.randomize());
$display("after cast pkt.count = %0d <= here the same pke is greater than 50 after casting",pkt.count);
$display("pkt type is %s",$typename(pkt));
$display("epkt type is %s",$typename(epkt));
end
endmodule