Polymorphism in SystemVerilog: One Handle, Many Classes (Tested)
In short: Polymorphism lets one handle type hold objects of many classes, and each object answers a method call its own way. In SystemVerilog this works through virtual methods: a tav_txn handle holding a tav_wr runs tav_wr‘s version. It is also why UVM factory overrides work, because the env keeps a base handle and the test decides which class sits behind it.
Every UVM testbench depends on polymorphism, even when nobody uses the word. A sequencer hands out “a sequence item”, and the driver works with whatever child class the test chose.
In this lesson we start with a tiny two-class example, then go further with a UVM bench of 8 checks, and remove one keyword to watch it break. I ran both on Synopsys VCS.
Start here: the simplest example
Two classes, two methods, one handle. print() is not virtual and vprint() is, and that one keyword is the whole difference.
// tav_poly_simple.sv : the smallest polymorphism example (UVM 1.2 reporting only)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 30 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
`include "uvm_macros.svh"
class tav_base;
function string print(); // NOT virtual
return "This is tav_base";
endfunction
virtual function string vprint(); // virtual
return "This is tav_base";
endfunction
endclass
class tav_child extends tav_base;
function string print();
return "This is tav_child";
endfunction
virtual function string vprint();
return "This is tav_child";
endfunction
endclass
module tav_top;
import uvm_pkg::*;
tav_base b;
tav_child c;
initial begin
b = new();
`uvm_info("TAV_SIMPLE", {"1 b holds a tav_base, b.print() : ", b.print()}, UVM_NONE)
c = new();
b = c; // the base handle now points at a child object
`uvm_info("TAV_SIMPLE", {"2 b holds a tav_child, b.print() : ", b.print()}, UVM_NONE) // not virtual
`uvm_info("TAV_SIMPLE", {"3 b holds a tav_child, b.vprint() : ", b.vprint()}, UVM_NONE) // virtual
end
endmodule
Here is what VCS printed:
UVM_INFO testbench.sv(33) @ 0: reporter [TAV_SIMPLE] 1 b holds a tav_base, b.print() : This is tav_base
UVM_INFO testbench.sv(37) @ 0: reporter [TAV_SIMPLE] 2 b holds a tav_child, b.print() : This is tav_base
UVM_INFO testbench.sv(38) @ 0: reporter [TAV_SIMPLE] 3 b holds a tav_child, b.vprint() : This is tav_child
- Line 2:
bnow holds atav_child, butprint()is not virtual, so the handle typetav_baseanswers. - Line 3:
vprint()is virtual, so the object answers:This is tav_child. That is polymorphism.
The rest of this lesson builds on these three lines. If they make sense, you already have the core idea.
What you will learn
- How one base handle can hold objects of many classes.
- Why the
virtualkeyword decides which method runs. - How to process mixed objects in one loop, with no
caseon the type. - The two kinds of polymorphism: run-time and compile-time.
- How interface classes and the UVM factory fit in.
New to classes? Start with inheritance in SystemVerilog, which this lesson builds on.
Cheat sheet: polymorphism in 6 rules
| Rule | Code | Why it matters |
|---|---|---|
| A base handle can hold any child object | tav_txn q[$]; q.push_back(wr); | One container, many classes |
Mark methods you want to override virtual | virtual function string kind(); | The object, not the handle, picks the method |
| Non-virtual methods follow the handle | q[i].name() returns "txn" | Easy to miss, and no error tells you |
Ask the object, not a case on its type | total += q[i].cost(); | New classes need no change to the loop |
Use $cast to ask “is it one of these?” | if ($cast(w, q[i])) ... | Safe check for a class and its children |
| Override through the factory in UVM | set_type_override(...) | The test swaps the class, the env code stays |
Analogy: a base handle is a phone number for “the support desk”. Who picks up depends on who is on shift, and each person answers in their own way, but you always dial the same number.
Two kinds of polymorphism
| Run-time | Compile-time | |
|---|---|---|
| How | Base handle plus virtual methods | Type parameters: class tav_util #(type T = int) |
| Decided | When the method is called | When the code is elaborated |
| In the bench | Checks 1, 3, 4, 7 and 8 | Check 6 |
| In UVM | Factory overrides, sequences, callbacks | uvm_driver #(REQ), uvm_sequencer #(REQ) |
q[i].kind() with and without virtual.Going further: one bench, 8 checks
- Class tree:
tav_txnis the base.tav_rdandtav_wrextend it, andtav_err_wrextendstav_wr. - Interface class:
tav_rdalsoimplements tav_printable. - Type parameter:
tav_util #(type T)has onetav_max()for any type that supports>. - UVM: the env holds a
tav_drvhandle, and the test overrides it withtav_err_drv. - Planted bug: the compile option
+define+TAV_BUGremovesvirtualfromtav_txn::kind().
Run it yourself: the same file is on GitHub, with the expected output from both runs.
// tav_poly_uvm.sv : polymorphism in SystemVerilog, a complete working example (UVM 1.2)
// The Art of Verification | theartofverification.com
// Run on Synopsys VCS X-2025.06-SP1 with UVM 1.2 (EDA Playground), 30 Nov 2026.
// EDA Playground: Synopsys VCS, UVM 1.2, top module "tav_top".
// Paste this whole file into testbench.sv and leave design.sv empty.
//
// What is in here:
// tav_txn / tav_rd / tav_wr / tav_err_wr a 3-level class tree, one base handle type for all
// tav_printable an interface class that tav_rd also implements
// tav_util #(type T) compile-time polymorphism with a type parameter
// tav_drv / tav_err_drv a UVM driver swapped by a factory override
// tav_poly_test 8 self-checks, each printed with `uvm_info and checked with `uvm_error
//
// Run options:
// (none) all 8 checks pass, 0 errors
// compile option +define+TAV_BUG tav_txn::kind() is declared WITHOUT virtual, so check 1 fails
// (in EDA Playground, put +define+TAV_BUG in the "Compile Options" box, not in "Run Options")
`include "uvm_macros.svh"
package tav_poly_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
// ---------------------------------------------------------------
// An interface class: a contract any class can implement
// ---------------------------------------------------------------
interface class tav_printable;
pure virtual function string tav_print();
endclass
// ---------------------------------------------------------------
// The class tree: tav_txn <- tav_rd, tav_txn <- tav_wr <- tav_err_wr
// ---------------------------------------------------------------
class tav_txn;
`ifdef TAV_BUG
function string kind(); // BUG: not virtual, so the handle type decides
`else
virtual function string kind(); // virtual: the object type decides
`endif
return "txn";
endfunction
function string name(); // never virtual, on purpose (check 2)
return "txn";
endfunction
virtual function int cost();
return 0;
endfunction
endclass
class tav_rd extends tav_txn implements tav_printable;
virtual function string kind(); return "rd"; endfunction
function string name(); return "rd"; endfunction
virtual function int cost(); return 1; endfunction
virtual function string tav_print(); return "RD"; endfunction
endclass
class tav_wr extends tav_txn;
virtual function string kind(); return "wr"; endfunction
function string name(); return "wr"; endfunction
virtual function int cost(); return 2; endfunction
endclass
class tav_err_wr extends tav_wr;
virtual function string kind(); return "err"; endfunction
function string name(); return "err"; endfunction
virtual function int cost(); return super.cost() + 10; endfunction // reuses tav_wr's cost
endclass
// ---------------------------------------------------------------
// Compile-time polymorphism: one piece of code, many types
// ---------------------------------------------------------------
class tav_util #(type T = int);
static function T tav_max(T a, T b);
return (a > b) ? a : b;
endfunction
endclass
// ---------------------------------------------------------------
// UVM: the env talks to a tav_drv handle, the test decides the class
// ---------------------------------------------------------------
class tav_drv extends uvm_component;
`uvm_component_utils(tav_drv)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
virtual function string tav_drive();
return "normal drive";
endfunction
endclass
class tav_err_drv extends tav_drv;
`uvm_component_utils(tav_err_drv)
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
virtual function string tav_drive();
return "error drive";
endfunction
endclass
class tav_env extends uvm_env;
`uvm_component_utils(tav_env)
tav_drv drv; // the env only knows the base type
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
drv = tav_drv::type_id::create("drv", this);
endfunction
endclass
// ---------------------------------------------------------------
// The test
// ---------------------------------------------------------------
class tav_poly_test extends uvm_test;
`uvm_component_utils(tav_poly_test)
tav_env env;
int tav_pass, tav_fail;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
tav_drv::type_id::set_type_override(tav_err_drv::get_type());
env = tav_env::type_id::create("env", this);
endfunction
function void tav_check(string what, string got, string exp);
if (got == exp) begin
tav_pass++;
`uvm_info("TAV_POLY", $sformatf("PASS %s: %s", what, got), UVM_LOW)
end
else begin
tav_fail++;
`uvm_error("TAV_POLY", $sformatf("FAIL %s: got %s, expected %s", what, got, exp))
end
endfunction
task run_phase(uvm_phase phase);
tav_txn q[$];
tav_rd rd = new();
tav_wr wr = new();
tav_err_wr ew = new();
tav_wr w;
tav_printable pr;
string s;
int total, n_wr;
phase.raise_objection(this);
q.push_back(rd); q.push_back(wr); q.push_back(ew); // one queue, three classes
// 1. Virtual method through base handles: each object answers for itself
s = "";
foreach (q[i]) s = {s, q[i].kind(), " "};
tav_check("1 q[i].kind() (virtual)", s, "rd wr err ");
// 2. Non-virtual method through the same handles: the handle type answers
s = "";
foreach (q[i]) s = {s, q[i].name(), " "};
tav_check("2 q[i].name() (not virtual)", s, "txn txn txn ");
// 3. Middle-level handle holding a grandchild object
w = ew;
tav_check("3 tav_wr handle -> tav_err_wr object, kind()", w.kind(), "err");
// 4. One loop, three costs, no if/case on the type
foreach (q[i]) total += q[i].cost();
tav_check("4 total cost (1 + 2 + (2+10))", $sformatf("%0d", total), "15");
// 5. $cast asks "is this object a tav_wr or a child of it?"
foreach (q[i]) if ($cast(w, q[i])) n_wr++;
tav_check("5 objects that are a tav_wr", $sformatf("%0d", n_wr), "2");
// 6. Compile-time polymorphism: the same tav_max for int and string
tav_check("6 tav_max for int / string",
$sformatf("%0d / %s", tav_util#(int)::tav_max(3, 9), tav_util#(string)::tav_max("ab", "cd")),
"9 / cd");
// 7. An interface class handle holding a tav_rd
pr = rd;
tav_check("7 tav_printable handle -> tav_print()", pr.tav_print(), "RD");
// 8. UVM: the env's tav_drv handle holds a tav_err_drv, and the env code never changed
tav_check("8 env.drv type / tav_drive()",
$sformatf("%s / %s", env.drv.get_type_name(), env.drv.tav_drive()),
"tav_err_drv / error drive");
`uvm_info("TAV_POLY", $sformatf("Checks: %0d pass, %0d fail", tav_pass, tav_fail), UVM_NONE)
phase.drop_objection(this);
endtask
endclass
endpackage
module tav_top;
import uvm_pkg::*;
import tav_poly_pkg::*;
initial run_test("tav_poly_test");
endmodule
How to run it
- Open EDA Playground and pick Synopsys VCS with UVM 1.2.
- Paste the file into
testbench.svand leavedesign.svempty. - Run it once with empty options.
- Put
+define+TAV_BUGin Compile Options, not Run Options, and run again. A missing keyword can only be planted at compile time.
What VCS returned
| # | Check | Result | What it proves |
|---|---|---|---|
| 1 | q[i].kind(), virtual | rd wr err | Each object answers for itself |
| 2 | q[i].name(), not virtual | txn txn txn | Without virtual, the handle type answers |
| 3 | tav_wr handle holding a tav_err_wr | err | Dispatch works through every level |
| 4 | Total cost() in one loop | 15 | 1 + 2 + (2 + 10), with super.cost() in the grandchild |
| 5 | Objects that $cast to tav_wr | 2 | tav_wr and its child pass, tav_rd does not |
| 6 | tav_max for int / string | 9 / cd | One piece of code, two types |
| 7 | tav_printable handle holding a tav_rd | RD | An interface class handle is polymorphic too |
| 8 | env’s tav_drv handle after the override | tav_err_drv / error drive | The test swapped the class, the env did not change |
Result: 8 pass, 0 fail. Here is the log from the clean run.
UVM_INFO @ 0: reporter [RNTST] Running test tav_poly_test...
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 1 q[i].kind() (virtual): rd wr err
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 2 q[i].name() (not virtual): txn txn txn
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 3 tav_wr handle -> tav_err_wr object, kind(): err
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 4 total cost (1 + 2 + (2+10)): 15
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 5 objects that are a tav_wr: 2
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 6 tav_max for int / string: 9 / cd
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 7 tav_printable handle -> tav_print(): RD
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 8 env.drv type / tav_drive(): tav_err_drv / error drive
UVM_INFO testbench.sv(195) @ 0: uvm_test_top [TAV_POLY] Checks: 8 pass, 0 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 12
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_POLY] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
The planted bug: one missing word
With +define+TAV_BUG, tav_txn::kind() is declared without virtual. Check 1 fails with got txn txn txn, and nothing in the run log says why.
UVM_INFO @ 0: reporter [RNTST] Running test tav_poly_test...
UVM_ERROR testbench.sv(142) @ 0: uvm_test_top [TAV_POLY] FAIL 1 q[i].kind() (virtual): got txn txn txn , expected rd wr err
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 2 q[i].name() (not virtual): txn txn txn
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 3 tav_wr handle -> tav_err_wr object, kind(): err
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 4 total cost (1 + 2 + (2+10)): 15
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 5 objects that are a tav_wr: 2
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 6 tav_max for int / string: 9 / cd
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 7 tav_printable handle -> tav_print(): RD
UVM_INFO testbench.sv(138) @ 0: uvm_test_top [TAV_POLY] PASS 8 env.drv type / tav_drive(): tav_err_drv / error drive
UVM_INFO testbench.sv(195) @ 0: uvm_test_top [TAV_POLY] Checks: 7 pass, 1 fail
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_objection.svh(1276) @ 0: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO /apps/vcsmx/vcs/X-2025.06-SP1//etc/uvm-1.2/src/base/uvm_report_server.svh(904) @ 0: reporter [UVM/REPORT/SERVER]
--- UVM Report Summary ---
** Report counts by severity
UVM_INFO : 11
UVM_WARNING : 0
UVM_ERROR : 1
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[TAV_POLY] 9
[TEST_DONE] 1
[UVM/RELNOTES] 1
Now look at check 3 in the same run: it still passed with err. The children declare kind() as virtual, so a call through a tav_wr handle still dispatches. Only calls through the base tav_txn handle lost it.
The lesson: virtual must be on the method in the class whose handle you call through, which is usually the base. Writing it only in the child classes does not help a base handle.
Common mistakes to avoid
| Mistake | What you see | Fix |
|---|---|---|
virtual missing in the base class | Base handles run the base version (+define+TAV_BUG) | Declare the method virtual in the base |
| Override has a different signature | Compile error, or a new method that never runs through the base handle | Copy the base prototype exactly |
| Calling a child-only method through a base handle | Compile error | Add it to the base as virtual, or $cast first |
A case on the object type in the loop | Every new class needs a code change | Put the behaviour in a virtual method |
Building UVM objects with new() | Factory overrides do nothing | Use type_id::create() |
How AI helps you master this
- Predict the dispatch: paste a small class tree and a loop, and ask which version runs for each object. Then check it on the simulator.
- Find missing virtuals: paste your base classes and ask which methods are overridden in children but not declared
virtual. - Replace a case: paste a
caseon a type field and ask how to move each branch into a virtual method.
Warning: a convincing AI answer is not the same as a correct engineering result. Check 3 surprises many people, and AI tools too, so run the code.
Interview questions on polymorphism
- What is polymorphism in SystemVerilog? One handle type holding objects of different classes, with each object running its own version of a virtual method.
- What happens if the base method is not virtual? A call through a base handle runs the base version, whatever the object is.
- What are the two kinds of polymorphism? Run-time, through virtual methods, and compile-time, through type parameters.
- How does the UVM factory use polymorphism? The code keeps a base handle, and the factory puts a child object behind it when a test sets an override.
- What is an interface class? A set of pure virtual methods that any class can
implement, even if it already extends another class.
More practice: SystemVerilog interview questions.
Practice challenge
- Run the bench and confirm 8 pass, 0 fail.
- Run with
+define+TAV_BUGand explain why check 3 still passes. - Add
tav_idle, which extendstav_txnwith a cost of 0, and push it into the queue. Which lines of the loop did you have to change? - Bonus: make
tav_wralso implementtav_printable. What do you have to add totav_err_wr?
FAQ
What is polymorphism in SystemVerilog? The ability of one base-class handle to hold objects of many child classes, with each object running its own version of a virtual method.
What are the two types of polymorphism in SystemVerilog? Run-time polymorphism, using virtual methods, and compile-time polymorphism, using parameterized classes.
What is the difference between a virtual and a non-virtual method? For a virtual method, the object’s class decides which version runs. For a non-virtual method, the handle’s class decides.
Can I call a child-only method through a base handle? No. Cast the handle to the child type with $cast first, or add the method to the base class.
Why does virtual matter for the UVM factory? The env calls methods through a base handle. Without virtual, the overridden class’s methods would never run.
Is an interface class the same as a virtual class? No. A class can extend only one virtual class, but it can implement several interface classes, as virtual vs pure virtual methods explains for abstract classes.
References
- IEEE 1800-2017, SystemVerilog standard: section 8.20 (virtual methods), 8.21 (abstract classes), 8.25 (parameterized classes), 8.26 (interface classes) and 8.16 (
$cast). - Accellera UVM 1.2 Class Reference:
uvm_factoryandset_type_override.
Keep learning
- The OOP set: inheritance, the this keyword and virtual vs pure virtual methods.
- Where UVM uses it: the UVM factory and create() vs new().
- Interview prep: SystemVerilog interview questions.
Part of the learning path SystemVerilog for verification

I guess in the above example, if we declared method as Virtual in Base Class as well as in Derived Class, the Base class print method will still point to the print method of Extended class, but I’m not completely sure about the reason behind it. Could you elaborate?
Hi Karthik,
You should try that experiment and find the reason why it’s like that. Then only you will understand better.
Thank you
Hi Hardik,
Could you please explain the difference between declaring method in both base class and child class as virtual , v/s just declaring base class method virtual.