What you practice
virtual methods (2.8). Enable polymorphism with virtual methods so child classes can override behavior. It is in Advanced OOP — Part 2 · Verification & OOP of the SystemVerilog Masterclass. Software-side verification: dynamic data structures, classes, and the first UVM master keys.
The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.
What you will learn
Today you will learn how to enable polymorphism in SystemVerilog by marking base class methods as virtual, allowing child classes to override their behavior when invoked through a base-class handle.
Learning objectives
- Understand the purpose of the
virtualkeyword in class method declarations. - Recognize how method overriding works via base-class handles.
- Implement
virtualmethods in a base class and override them in a derived class. - Verify correct polymorphic behavior using a testbench.
Theory
In SystemVerilog, methods in a base class are non-virtual by default, meaning the method called is determined at compile time based on the handle type—not the object type. The virtual keyword enables dynamic dispatch: the method called is determined at runtime based on the actual object, not the handle type.
| Keyword | Behavior |
|---|---|
function | Static dispatch: method bound at compile time |
virtual function | Dynamic dispatch: method bound at runtime (polymorphism) |
When a child class overrides a virtual method, calling that method through a base-class handle executes the child’s version—this is the core of object-oriented polymorphism.
Practice
Open this chapter in RtlStudio. Files appear automatically in Explore.
- Open
student/tb/tb.svfrom the Explore tree. - In the
baseclass, add thevirtualkeyword beforefunction display(). - In the
childclass, add thevirtualkeyword beforefunction display(). - Run Simulation and check the Log panel for expected output.
Summary
You practiced using the virtual keyword to enable polymorphism in SystemVerilog, allowing child classes to override base class methods. By declaring a method as virtual in the base class, you ensured that a parent handle pointing to a child object correctly invokes the child’s implementation at runtime. This reinforces the core OOP principle that behavior can be customized in derived classes while maintaining a consistent interface. You confirmed this behavior in your testbench by calling display() through a base-class reference and observing the child’s version execute.
Run it in the Verilog IDE
- Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
- Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
- Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
- Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.
The lesson is done when the simulation matches virtual methods. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.