User guide
24-53
SystemVerilog Testbench Constructs
class MyPacket extends Packet;
task display();
$display("This is the display within
MyPacket");
endtask
endclass
class YourPacket extends Packet;
task display();
$display("This is the display within
YourPacket");
endtask
endclass
This example illustrates how the two classes derived from Packet
implement their own specific version of the display() method.
All derived classes can be referenced by a base class object. When
a derived class is referenced by the base class, the base class can
access the virtual methods within the derived class through the
handle of the base class.
program polymorphism;
//include class declarations of Packet, MyPacket
//and YourPacket here
MyPacket mp;
YourPacket yp;
Packet p; //abstract base class
initial
begin
mp = new;
yp = new;
p = mp; // mp referenced by p
p.display();// calls display in MyPacket
p = yp;
p.display();// calls display in YourPacket
end
endprogram
Output: