User guide
24-52
SystemVerilog Testbench Constructs
Methods of non-abstract classes can also be declared virtual. In this
case, the method must have a body, since the class, and its
subclasses, must be able to be instantiated. It should be noted that
once a method is declared as “virtual,” it is forever virtual. Therefore,
the keyword does not need to be used in the redefinition of a virtual
method in a subclass.
Polymorphism
The ability to call a variety of functions using the exact same interface
is called polymorphism.
Suppose we have a class called Packet. Packet has a task called
display(). All the derived classes of Packet will also have a display()
task, but the base version of display() does not satisfy the needs of
the derived classes. In such a case we want the derived class to
implement its own version of the display() task to be called.
To achieve polymorphism the base class must be abstract (defined
using the virtual identifier) and the method(s) of the class must be
defined as virtual. The abstract class (see page 50 for definition of
abstract class) serves as a template for the construction of derived
classes.
virtual class Packet;
extern virtual task display();
endclass
The above code snippet defines an abstract class, Packet. Within
Packet the virtual task, display(), has been defined. display() serves
as a prototype for all classes derived from Packet. Any class which
derives from Packet() must implement the display() task. Note that
the prototype is enforced for each derived task (that is, must keep
the same arguments, etc.).