Inheritance, Virtual Methods & Polymorphism

extends, super, virtual and pure virtual methods, abstract classes and $cast — how testbench components are customized.

Inheritance lets a class extend another: it gets all the parent's fields and methods and can add or replace them. With virtual methods, code that holds a base-class handle calls the right version for the actual object. This is how verification frameworks are built: the framework calls methods of base classes, and your test supplies subclasses.

A transaction hierarchy

virtual class Transaction cannot be created with new, only extended.

super.new(id) calls the parent's constructor; it must be the first statement.

Burst extends Read and reuses Read's describe() through super.describe().

list holds base-class handles to objects of three different classes.

$cast(back, list[1]) succeeds only if the object really is a Burst.

Why virtual matters

function string describe(); // not virtual in the base virtual function string describe();

Without virtual, calls through a base-class handle run the base version, and your subclass's override is silently ignored.

Burst b = list[1]; // compile error Burst b; if ($cast(b, list[1]))...

Assigning a base handle to a subclass handle needs a checked cast, because the object might be of another subclass.

What is polymorphism in a testbench context?

Treating objects of different subclasses through a common base-class handle and having each run its own version of a virtual method — e.g. a driver processing reads, writes and bursts through one Transaction handle.

What is an abstract class?

A class declared virtual class: it cannot be instantiated, only extended. It typically declares pure virtual methods that subclasses must implement.