Inheritance in SystemVerilog: extends, super and virtual (Tested)
In short: Inheritance lets one SystemVerilog class (the child) reuse and extend another (the parent) with extends. The child gets the parent’s fields, methods and constraints, can add its own, and can override methods. Call super.new() to run the parent constructor and super.method() to reuse parent code, and mark methods virtual so a parent handle runs the child’s version.
Every UVM testbench is built on inheritance. Your driver extends uvm_driver, your transaction extends uvm_sequence_item, and an error-injecting transaction usually extends a clean one.
In this lesson we test 9 inheritance behaviours in one UVM bench, from constructor order to $cast. I ran every check on Synopsys VCS.
Start here: the simplest example
One parent, one child. The child adds a field of its own and reuses the parent’s describe() with super.
// tav_inherit_simple.sv : the smallest inheritance 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_pkt;
int tav_len = 4;
function string describe();
return $sformatf("len=%0d", tav_len);
endfunction
endclass
class tav_err_pkt extends tav_pkt; // gets tav_len and describe() for free
bit tav_err = 1; // adds its own field
function string describe();
return {super.describe(), $sformatf(" err=%0d", tav_err)}; // reuses the parent's text
endfunction
endclass
module tav_top;
import uvm_pkg::*;
tav_pkt p;
tav_err_pkt e;
initial begin
p = new();
e = new();
`uvm_info("TAV_SIMPLE", {"1 parent p.describe() : ", p.describe()}, UVM_NONE)
`uvm_info("TAV_SIMPLE", {"2 child e.describe() : ", e.describe()}, UVM_NONE)
`uvm_info("TAV_SIMPLE", $sformatf("3 child e.tav_len : %0d (inherited)", e.tav_len), UVM_NONE)
end
endmodule
Here is what VCS printed:
UVM_INFO testbench.sv(30) @ 0: reporter [TAV_SIMPLE] 1 parent p.describe() : len=4
UVM_INFO testbench.sv(31) @ 0: reporter [TAV_SIMPLE] 2 child e.describe() : len=4 err=1
UVM_INFO testbench.sv(32) @ 0: reporter [TAV_SIMPLE] 3 child e.tav_len : 4 (inherited)
- Line 2: the child’s
describe()starts with the parent’s text throughsuper.describe(), then addserr=1. - Line 3:
tav_err_pktnever declarestav_len. It inherited it fromtav_pkt.
The rest of this lesson builds on this. The full bench further down tests the same idea from more angles, with a planted bug.
What you will learn
- How
extendsandsuperwork, and in what order constructors run. - Why a parent handle runs the child’s method only when it is
virtual. - How a child replaces or adds constraints.
- Why a field with the same name hides the parent’s field instead of overriding it.
- How to cast up and down safely with
$cast.
Cheat sheet: inheritance in 7 rules
| Rule | Code | Why it matters |
|---|---|---|
| Extend a class | class tav_err_item extends tav_base_item; | The child gets every field, method and constraint |
| Call the parent constructor first | super.new(name); | The parent part is set up before the child part |
| Make overridable methods virtual | virtual function string describe(); | A parent handle then runs the child’s version |
| Reuse parent code in an override | return {super.describe(), ...}; | Extend behaviour instead of copying it |
| Replace a constraint by name | constraint c_len { len == 8; } | The same name in the child replaces the parent’s |
| Upcast freely | b = e; | A parent handle can always hold a child object |
| Downcast with a check | if ($cast(e2, b)) ... | It fails at run time if the object is not a child |
The words you need
| Term | Meaning |
|---|---|
| Parent, base class, superclass | The class being extended |
| Child, derived class, subclass | The class that extends it |
| Override | A child method with the same name and signature as a parent method |
super | A handle to the parent part of the current object |
| Upcast | Child handle to parent handle. Always allowed |
| Downcast | Parent handle to child handle. Needs $cast |
b.Virtual vs non-virtual: who decides?
Call through a parent handle b holding a child object | Not virtual | Virtual |
|---|---|---|
| Which version runs | The parent’s (the handle type decides) | The child’s (the object type decides) |
| In the bench | b.kind() returned "base" | b.vkind() returned "err" |
- Once virtual, always virtual: an override of a virtual method is virtual too, even without the keyword.
- Fields are never virtual: a child field with the same name hides the parent’s field. A parent handle still sees the parent’s value.
Analogy: a parent handle is like a job title on a badge. A non-virtual method asks the badge what to do, so you get the generic answer. A virtual method asks the person wearing it, so you get the specialist’s answer.
What a child can and cannot see
| Member is | Visible in the child? | Visible outside the class? |
|---|---|---|
| Public (default) | Yes | Yes |
protected | Yes | No |
local | No | No |
Single inheritance only: a SystemVerilog class extends exactly one class. Interface classes cover the “several contracts” case.
Inheritance vs composition
Inheritance (extends) | Composition (a handle inside) | |
|---|---|---|
| Relationship | “is a”: an error item is an item | “has a”: an env has an agent |
| Best for | Variants of one thing, factory overrides | Building bigger blocks from parts |
| Watch out for | Deep trees that are hard to follow | More wiring code |
Going further: one bench, 9 checks
I ran this exact file on Synopsys VCS X-2025.06 with UVM 1.2. Every user name uses the tav_ prefix and every message goes through uvm_info.
Run it yourself: the same file is on GitHub. Paste it into testbench.sv on EDA Playground, leave design.sv empty, pick VCS and UVM 1.2, and set the top module to tav_top.
| Check | What it tests | VCS result |
|---|---|---|
| 1 | Constructor order with super.new() | base.new err.new |
| 2 | b.kind(), not virtual, parent handle | base |
| 3 | b.vkind(), virtual, parent handle | err |
| 4 | len after b.randomize() (child replaced c_len) | 8 |
| 5 | err after b.randomize() (child added c_err) | 1 |
| 6 | b.describe(), virtual override calling super | len=8 err=1 |
| 7 | Shadowed tav_id through parent / child handle | 1/2 |
| 8 | $cast(e2, b) when b holds a child | 1 |
| 9 | $cast(e3, b2) when b2 holds a base object | 0 |
Planted bug: +BUG=1 makes the child’s describe() skip super.describe(). It compiles and runs without a warning, and only check 6 notices that the len part is gone.
// tav_inherit_uvm.sv : inheritance 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_base_item a UVM transaction: rand len, a constraint, a normal method and two virtual methods
// tav_err_item extends it: adds an err field, replaces the len constraint, overrides methods,
// calls super, and shadows one data member (a classic trap)
// tav_inherit_test 9 self-checks, each printed with `uvm_info and checked with `uvm_error:
// 1 constructor order 2 non-virtual call 3 virtual call 4-5 constraint override
// 6 super in an override 7 shadowed member 8 $cast down (works) 9 $cast down (fails)
//
// Run options:
// (none) all 9 checks pass, 0 errors
// +BUG=1 tav_err_item::describe() forgets super.describe(), so check 6 fails
`include "uvm_macros.svh"
package tav_inherit_pkg;
import uvm_pkg::*;
`include "uvm_macros.svh"
// ---------------------------------------------------------------
// Base class
// ---------------------------------------------------------------
class tav_base_item extends uvm_sequence_item;
`uvm_object_utils(tav_base_item)
rand bit [7:0] len;
int tav_id = 1; // the child declares a field with the same name
static string tav_trace; // records the constructor order
constraint c_len { len inside {[1:4]}; }
function new(string name = "tav_base_item");
super.new(name);
tav_trace = {tav_trace, "base.new "};
endfunction
// Not virtual: the HANDLE type picks which version runs
function string kind();
return "base";
endfunction
// Virtual: the OBJECT type picks which version runs
virtual function string vkind();
return "base";
endfunction
virtual function string describe();
return $sformatf("len=%0d", len);
endfunction
endclass
// ---------------------------------------------------------------
// Derived class
// ---------------------------------------------------------------
class tav_err_item extends tav_base_item;
`uvm_object_utils(tav_err_item)
rand bit err;
int tav_id = 2; // shadows tav_base_item::tav_id
int tav_bug;
constraint c_len { len == 8; } // same name as the base constraint: replaces it
constraint c_err { err == 1; } // new constraint: added on top
function new(string name = "tav_err_item");
super.new(name); // the base constructor runs first
tav_trace = {tav_trace, "err.new "};
endfunction
function string kind();
return "err";
endfunction
virtual function string vkind();
return "err";
endfunction
// Extend the parent's behaviour instead of copying it
virtual function string describe();
if (tav_bug == 1)
return $sformatf("err=%0d", err); // BUG: parent part lost
return {super.describe(), $sformatf(" err=%0d", err)};
endfunction
endclass
// ---------------------------------------------------------------
// The test
// ---------------------------------------------------------------
class tav_inherit_test extends uvm_test;
`uvm_component_utils(tav_inherit_test)
int tav_pass, tav_fail, tav_bug;
function new(string name, uvm_component parent);
super.new(name, parent);
endfunction
function void build_phase(uvm_phase phase);
super.build_phase(phase);
void'($value$plusargs("BUG=%d", tav_bug));
endfunction
function void tav_check(string what, string got, string exp);
if (got == exp) begin
tav_pass++;
`uvm_info("TAV_OOP", $sformatf("PASS %s: \"%s\"", what, got), UVM_LOW)
end
else begin
tav_fail++;
`uvm_error("TAV_OOP", $sformatf("FAIL %s: got \"%s\", expected \"%s\"", what, got, exp))
end
endfunction
task run_phase(uvm_phase phase);
tav_err_item e, e2, e3;
tav_base_item b, b2;
phase.raise_objection(this);
// 1. Constructor order: super.new() runs the parent body first
tav_base_item::tav_trace = "";
e = tav_err_item::type_id::create("e");
e.tav_bug = tav_bug;
tav_check("1 constructor order", tav_base_item::tav_trace, "base.new err.new ");
// Upcast: a parent handle can hold a child object, no cast needed
b = e;
// 2. Non-virtual method through a parent handle: the handle type decides
tav_check("2 b.kind() (not virtual)", b.kind(), "base");
// 3. Virtual method through a parent handle: the object type decides
tav_check("3 b.vkind() (virtual)", b.vkind(), "err");
// 4-5. Constraint override: randomize through the parent handle,
// the child's constraints still apply
if (!b.randomize()) `uvm_error("TAV_OOP", "randomize() failed")
tav_check("4 len after randomize (c_len replaced)", $sformatf("%0d", e.len), "8");
tav_check("5 err after randomize (c_err added)", $sformatf("%0d", e.err), "1");
// 6. super in an override: the child adds to the parent's text
tav_check("6 b.describe() (virtual + super)", b.describe(), "len=8 err=1");
// 7. Shadowed data member: fields are NOT virtual, the handle type decides
tav_check("7 shadowed tav_id via parent / child handle",
$sformatf("%0d/%0d", b.tav_id, e.tav_id), "1/2");
// 8. Downcast that works: b really holds a tav_err_item
tav_check("8 $cast(e2, b) on a child object", $sformatf("%0d", $cast(e2, b)), "1");
// 9. Downcast that fails safely: b2 holds a plain base object
b2 = tav_base_item::type_id::create("b2");
tav_check("9 $cast(e3, b2) on a base object", $sformatf("%0d", $cast(e3, b2)), "0");
`uvm_info("TAV_OOP", $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_inherit_pkg::*;
initial run_test("tav_inherit_test");
endmodule
What the run shows
What to notice:
- Check 1: the parent constructor body ran before the child’s.
- Checks 2 and 3: the same parent handle gave
"base"for the plain method and"err"for the virtual one. - Checks 4 and 5: randomizing through the parent handle still used the child’s constraints.
- Check 7: the object holds two
tav_idfields, and the handle type picks which one you see. VCS gave no warning about it. - Result: 9 pass, 0 fail, 0
UVM_ERROR.
Here is the clean run from VCS:
UVM_INFO @ 0: reporter [RNTST] Running test tav_inherit_test...
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 1 constructor order: "base.new err.new "
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 2 b.kind() (not virtual): "base"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 3 b.vkind() (virtual): "err"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 4 len after randomize (c_len replaced): "8"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 5 err after randomize (c_err added): "1"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 6 b.describe() (virtual + super): "len=8 err=1"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 7 shadowed tav_id via parent / child handle: "1/2"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 8 $cast(e2, b) on a child object: "1"
UVM_INFO testbench.sv(111) @ 0: uvm_test_top [TAV_OOP] PASS 9 $cast(e3, b2) on a base object: "0"
UVM_INFO testbench.sv(159) @ 0: uvm_test_top [TAV_OOP] Checks: 9 pass, 0 fail
--- UVM Report Summary ---
UVM_INFO : 13
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
And the line that changes with +BUG=1:
UVM_ERROR testbench.sv(115) @ 0: uvm_test_top [TAV_OOP] FAIL 6 b.describe() (virtual + super): got "err=1", expected "len=8 err=1"
UVM_INFO testbench.sv(159) @ 0: uvm_test_top [TAV_OOP] Checks: 8 pass, 1 fail
UVM_ERROR : 1
One check, two simulators
Before the VCS run, I tried the same checks without UVM in Verilator 5.020. Seven matched, but the two randomize checks did not: Verilator returned len=196 err=0, ignoring both the replaced and the added constraint. VCS applied both, as the standard says.
The lesson: constraint inheritance is a place where tool support differs. If a child class exists only to change constraints, add a check that the new values really come out.
Common mistakes to avoid
| Mistake | What you see | Fix |
|---|---|---|
Override without virtual in the parent | A parent handle runs the parent’s version (check 2) | Declare the parent method virtual |
Override forgets super.method() | The parent’s part of the work silently disappears (+BUG=1) | Call super.method() when you mean to extend |
| Child declares a field with the parent’s name | Two copies, and the parent handle sees the old one (check 7) | Use a new name, or set the parent’s field |
| Downcast with plain assignment | Compile error | Use $cast(child, parent) |
$cast used as a task on the wrong object | Run-time error | Use the function form and check the return value |
| Child adds a constraint that conflicts with the parent’s | randomize() fails | Reuse the parent constraint’s name to replace it |
| A deep class tree for every small variant | Code nobody can follow | Use constraints, config fields or composition for small variants |
How AI helps you master this
- Predict the dispatch: paste a small class tree and ask which method runs for each handle. Then check it on the simulator.
- Spot a missing super: paste an override and ask what parent behaviour it drops.
- Generate a test variant: describe an error-injection item and ask for a child class that replaces only the needed constraints.
Warning: a convincing AI answer is not the same as a correct engineering result. AI tools sometimes claim fields are overridden like virtual methods, and check 7 shows they are not. Run the code.
Interview questions on inheritance
- What happens if you override a non-virtual method and call it through a parent handle? The parent’s version runs. The handle type decides.
- Why must
super.new()come first in a constructor? The parent part of the object has to be built before the child part uses it. - How do you replace a parent constraint in a child? Declare a constraint with the same name in the child.
- What is the difference between
protectedandlocal? A child can seeprotectedmembers, but notlocalones. - Can a SystemVerilog class extend two classes? No. Classes have single inheritance. Use interface classes for several contracts.
More practice: SystemVerilog interview questions.
Practice challenge
- Run the bench and confirm 9 pass, 0 fail.
- Run with
+BUG=1and explain what disappeared from check 6. - Make
kind()virtual in the base class and predict the new result of check 2. - Bonus: add
tav_long_err_item, which extendstav_err_itemand allowslenfrom 9 to 16. What doesb.describe()print now?
FAQ
What is inheritance in SystemVerilog? A way for one class to reuse another class’s fields, methods and constraints with extends, then add or override its own.
Why do I need super.new()? It runs the parent constructor so the parent part of the object is set up. If the parent constructor needs arguments, you must pass them.
What does virtual actually change? Which version of a method runs through a parent handle. Without virtual, the handle type decides. With it, the object type decides.
Can a SystemVerilog class inherit from more than one class? No. Each class extends one class. Interface classes allow a class to implement several contracts.
What is the difference between local and protected? protected members are visible in child classes. local members are visible only inside the class that declares them.
How does UVM use inheritance? Every component and transaction extends a UVM base class, and factory overrides swap a class for its child without changing the test code.
References
- IEEE 1800-2017, SystemVerilog standard: section 8 (classes), including inheritance,
super, virtual methods, data hiding and$cast; section 18.5.2 (constraint inheritance).
Keep learning
- Next in the OOP set: polymorphism, encapsulation, and virtual vs pure virtual methods.
- The current object: the this keyword.
- Where UVM uses this: the UVM factory and overrides.
- Interview prep: SystemVerilog interview questions.
Part of the learning path SystemVerilog for verification
