An interface can be instantiated inside a module, but never inside a class. That's a real problem the moment you start building an object-oriented testbench: a class-based driver, monitor, or scoreboard still needs to drive and sample real DUT signals somehow. A virtual interface is the handle that closes that gap — a class-compatible reference that points at an actual interface instance created elsewhere, letting class code reach out into the physical world without ever instantiating an interface itself.
7 min read | Intermediate Level
What You'll Learn
- Why a class can't directly instantiate an interface, and what problem virtual interfaces solve
- The syntax for declaring a virtual interface handle, with or without a modport
- How to pass a virtual interface into a class through its constructor
- How virtual interfaces work together with modports and clocking blocks
- The most common pitfall: using an uninitialized (null) virtual interface handle
Why Do We Need Virtual Interfaces?
SystemVerilog Interface covers how an interface bundles signals together, and SystemVerilog Modport covers restricting access to those signals per module. Both of those are still module-side constructs, though — a class cannot declare an interface instance as one of its own properties. Since class-based components (drivers, monitors, scoreboards, and similar) are exactly where most of a modern testbench's logic lives, there has to be some way for that class code to reach the actual interface signals living out in a module.
A virtual interface is that bridge. It's a handle — conceptually similar to a class handle — that can be declared as a class property, and later assigned to point at a real interface instance. Nothing about declaring one creates any new signals; it's just a reference, exactly like an unassigned class handle is just a reference until you call new() on it.
Basic Syntax
virtual interface_name variable_name;
// Restricted to a specific modport's view, same as a module port would be
virtual interface_name.modport_name variable_name;
Both forms declare a handle, not a new interface instance. The second form, with a modport, restricts what the class can access through that handle to exactly what the named modport allows — the same restriction a module would get by declaring its port as interface_name.modport_name instead of the whole interface.
A Complete Example
The interface below reuses the request-grant pattern from earlier in this chapter, with a clocking block and a TB modport that references it:
interface req_gnt_if (input logic clk);
logic req;
logic gnt;
logic rstn;
clocking cb @(posedge clk);
default input #1step output #2ns;
output req;
input gnt;
endclocking
modport TB (clocking cb, output rstn);
endinterface
A simple Driver class stores a virtual interface handle, typed to the TB modport, and takes it in as a constructor argument:
class Driver;
virtual req_gnt_if.TB vif; // handle only - no signals of its own
function new (virtual req_gnt_if.TB vif);
this.vif = vif;
endfunction
task drive_req (bit value);
vif.cb.req <= value;
endtask
endclass
The design and testbench modules stay exactly as they'd look without any class involved — the only new piece is passing vif into Driver's constructor:
module des (input logic clk, req, rstn, output logic gnt);
always @ (posedge clk or negedge rstn)
if (!rstn) gnt <= 0;
else gnt <= req;
endmodule
module tb;
logic clk;
always #10 clk = ~clk;
req_gnt_if vif (.clk(clk));
des dut (.clk(clk), .req(vif.req), .rstn(vif.rstn), .gnt(vif.gnt));
Driver drv;
initial begin
vif.rstn <= 0;
repeat (3) @(posedge clk);
vif.rstn <= 1;
// Pass the physical interface instance into the class through its constructor -
// SystemVerilog automatically converts it to a virtual interface handle here
drv = new (vif);
repeat (2) @(posedge clk);
drv.drive_req(1);
repeat (5) @(posedge clk);
$finish;
end
endmodule
vif is declared in the module as a plain interface instance, exactly as in every other example in this chapter. Passing it into new(vif) is where the conversion happens: SystemVerilog automatically treats an actual interface instance as a valid value for a virtual interface parameter, so no explicit cast is needed. From that point on, drv.vif behaves like any other class handle — it can be reassigned, passed to other methods, or stored in an array of handles.
Quick Check
Q: Does declaring virtual req_gnt_if.TB vif; inside the Driver class create a new set of req/gnt/rstn signals?
Show Answer
No. It only declares a handle — a reference that can later point at an already-existing interface instance. No signals exist until the handle is assigned to point at a real interface, the same way a class handle doesn't create an object until new() is called on it.
The Most Common Pitfall: An Uninitialized Virtual Interface
A virtual interface handle that's declared but never assigned defaults to null, exactly like an unassigned class handle. Using it before assignment produces a run-time error — not a compile-time one — because the compiler has no way of knowing at compile time whether the handle will actually be assigned before it's used. Suppose Driver had no constructor argument at all, and nothing else in the testbench ever assigned vif:
class Driver;
virtual req_gnt_if.TB vif; // starts out null - nothing assigned yet
task drive_req (bit value);
vif.cb.req <= value; // fails at run time if vif was never assigned
endtask
endclass
module tb;
Driver drv;
initial begin
drv = new(); // vif was never passed in or assigned anywhere!
drv.drive_req(1); // run-time error: dereferencing a null handle
end
endmodule
This is one of the most common real-world virtual interface bugs, especially in UVM-style environments where the virtual interface is often set from the top-level testbench and retrieved deep inside the class hierarchy through a configuration database. If that set/get pairing is missed or mistyped anywhere along the way, the handle stays null, and the failure only shows up much later, at run time, the first time the class actually tries to use it.
Where Are Virtual Interfaces Used?
Virtual interfaces are the standard mechanism by which class-based driver, monitor, and scoreboard components in an object-oriented testbench reach the DUT at all — in UVM specifically, a virtual interface is typically set once from the top-level testbench and retrieved by name wherever a component needs it, rather than being passed through every constructor by hand.
Virtual Interfaces with Modports and Clocking Blocks
A virtual interface handle typed to a specific modport, like virtual req_gnt_if.TB above, restricts the class to that modport's view — exactly as if a module had declared that same modport as its port type. Accessing signals through the interface's clocking block works identically through a virtual handle as it does through a plain interface instance: vif.cb.req applies the clocking block's output skew whether vif is a module's local interface instance or a class's virtual interface handle pointing at one.
Common Beginner Mistakes
Mistake #1: Using a virtual interface handle before it's assigned
As shown above, a virtual interface handle defaults to null until something assigns it to point at a real interface instance. Why? Any attempt to access a signal through a null handle fails at run time, and since this compiles fine, the bug only surfaces the first time that code path actually executes — which can be well into a test run.
Mistake #2: Declaring the virtual interface without a modport
A plain virtual req_gnt_if vif; gives the class unrestricted access to every signal in the interface, including ones it was never meant to touch. Why? Typing the handle to a specific modport, like virtual req_gnt_if.TB, restricts the class to exactly the signals and directions that modport allows — the same access control a module gets from using a modport on its own port.
Mistake #3: Expecting a virtual interface declaration to create new signals
Declaring a virtual interface property inside a class doesn't instantiate anything. Why? It's a handle, not a new interface — the actual signals only exist because some module somewhere instantiated the real interface; the virtual interface just gives class code a way to reach them.
Practice Exercise
Challenge: Write a Monitor Class
Using the req_gnt_if interface from earlier, write a Monitor class that stores a virtual interface handle (typed to the TB modport) and has a task sample_gnt(gnt_val) that waits for one clock edge and captures the current value of gnt into its output argument.
Hint
Follow the same pattern as Driver: a virtual interface property, a constructor that assigns it, and a task that accesses the signal through vif.cb. Since the task needs to wait on a clock edge, it has to stay a task, not a function — so use an output argument to hand back the sampled value instead of a return type.
Solution
class Monitor;
virtual req_gnt_if.TB vif;
function new (virtual req_gnt_if.TB vif);
this.vif = vif;
endfunction
task sample_gnt (output bit gnt_val);
@(vif.cb);
gnt_val = vif.cb.gnt;
endtask
endclass
module tb;
// ... clk, req_gnt_if vif, and des dut instantiated as before
Monitor mon;
bit g;
initial begin
mon = new (vif);
mon.sample_gnt(g);
$display("gnt = %0b", g);
end
endmodule
sample_gnt has to be a task, not a function, because @(vif.cb) consumes simulation time — functions can't contain timing controls. That's also why the sampled value comes back through an output argument instead of a function return value.
Summary
In this tutorial, you learned:
- A class can never instantiate an interface directly — a virtual interface is the handle that lets class code reach an interface instance created elsewhere
- Declaring a virtual interface only creates a reference, not new signals; it defaults to
nulluntil assigned - An actual interface instance is automatically usable wherever a virtual interface of a matching type is expected, including as a constructor argument
- Typing a virtual interface to a specific modport restricts class access to exactly that modport's signals and directions
- Accessing a clocking block through a virtual interface handle applies the same input/output skew as accessing it through a plain interface instance
- Using an unassigned (null) virtual interface handle is one of the most common real-world virtual interface bugs, and only fails at run time
At times it's much easier to write a generic class that can be instantiated in multiple ways to achieve different array sizes or data types. This avoids rewriting nearly-identical code for specific features like size or type, letting a single specification serve many different objects instead — achieved by extending SystemVerilog's parameter mechanism to classes.
Parameters are like constants local to the specified class. Classes are allowed a default value for each parameter, which can be overridden at instantiation.
9 min read | Intermediate Level
What You'll Learn
- Why parameterizing a class avoids rewriting nearly-identical code for different sizes or types
- How to override a class parameter's value, or its entire data type, at instantiation
- How the same parameter mechanism controls a value's size in one case, and a completely different data type in another
- Why assigning a value into a narrower parameterized type silently truncates it, rather than raising an error
Syntax
// Declare parameterized class
class #();
class Trans #(addr = 32);
// Override class parameter
#() ;
Trans #(.addr(16)) obj;
Parameterizing a Value (Size)
Here, size is the parameter, defaulting to 8, and controls how wide the out property is:
// A class is parameterized by #()
// Here, we define a parameter called "size" and gives it
// a default value of 8. The "size" parameter is used to
// define the size of the "out" variable
class something #(int size = 8);
bit [size-1:0] out;
endclass
module tb;
// Override default value of 8 with the given values in #()
something #(16) sth1; // pass 16 as "size" to this class object
something #(.size (8)) sth2; // pass 8 as "size" to this class object
typedef something #(4) td_nibble; // create an alias for a class with "size" = 4 as "nibble"
td_nibble nibble;
initial begin
// 1. Instantiate class objects
sth1 = new;
sth2 = new;
nibble = new;
// 2. Print size of "out" variable. $bits() system task will return
// the number of bits in a given variable
$display ("sth1.out = %0d bits", $bits(sth1.out));
$display ("sth2.out = %0d bits", $bits(sth2.out));
$display ("nibble.out = %0d bits", $bits(nibble.out));
end
endmodule
Output
ncsim> run sth1.out = 16 bits sth2.out = 8 bits nibble.out = 4 bits ncsim: *W,RNQUIE: Simulation is complete.
Note that sth2 explicitly restates the default value (8), showing the two equivalent ways of overriding a parameter: positional (#(16)) or named (#(.size(8))). td_nibble shows a third pattern — using typedef to give a specific parameterization of the class its own convenient name.
Quick Check
Q: If a fourth object were declared as plain something sth3;, with no #() at all, how many bits would sth3.out have?
Show Answer
8 bits — the parameter's own declared default value, exactly like leaving any other default argument unspecified.
Parameterizing a Data Type
Instead of parameterizing a specific value, a class can parameterize an entire data type, using the type keyword:
// "T" is a parameter that is set to have a default value of "int"
// Hence "items" will be "int" by default
class stack #(type T = int);
T item;
function T add_a (T a);
return item + a;
endfunction
endclass
module tb;
stack st; // st.item is by default of int type
stack #(bit[3:0]) bs; // bs.item will become a 4-bit vector
stack #(real) rs; // rs.item will become a real number
initial begin
st = new;
bs = new;
rs = new;
// Assign different values, and add 10 to these values
// Then print the result - Note the different values printed
// that are affected by change in data type
st.item = -456;
$display ("st.item = %0d", st.add_a (10));
bs.item = 8'hA1;
$display ("bs.item = %0d", bs.add_a (10));
rs.item = 3.14;
$display ("rs.item = %0.2f", rs.add_a (10));
end
endmodule
Any type can be supplied as a parameter here, including a user-defined type such as a class or struct.
Output
ncsim> run st.item = -446 bs.item = 11 rs.item = 13.14 ncsim: *W,RNQUIE: Simulation is complete.
Each line here actually prints the return value of add_a(10), not the object's item property directly — the label just happens to say "item" for readability. bs is the interesting one to trace through: T is bit[3:0] (4 bits, range 0–15), so assigning 8'hA1 (161 decimal) into bs.item truncates it down to just the lower 4 bits — 0x1 — before anything else happens. add_a(10) then computes 1 + 10 = 11, which fits comfortably within 4 bits, matching the printed 11.
Quick Check
Q: If bs.item were assigned 8'hAF instead of 8'hA1, what would bs.add_a(10) print?
Show Answer
9. 8'hAF truncates to the lower 4 bits, 1111 (15 decimal). Adding 10 gives 25, which no longer fits in 4 bits — since add_a()'s return type is also T (4 bits), the result is truncated the same way, keeping only the lower 4 bits of 25 (11001 binary): 1001, or 9 decimal.
Where Are Parameterized Classes Used?
UVM's own sequence and sequencer base classes are heavily parameterized — uvm_sequencer#(REQ, RSP), for instance — letting the exact same infrastructure work across completely different transaction types from one project to the next, without any of that shared code needing to know in advance what type it will eventually be used with.
Common Beginner Mistakes
Mistake #1: Assuming a value that doesn't fit raises an error
As the bs.item example shows, assigning a wider value into a narrower parameterized type silently truncates it instead of raising an error or warning. Why? This can produce results that look correct at a glance but are actually based on a truncated value — always double-check the parameterized width actually fits the values you intend to use.
Mistake #2: Misreading what a $display statement is actually printing
As the note above points out, $display("bs.item = %0d", bs.add_a(10)) prints the return value of a method call, not the object's current item property. Why? A display label doesn't have to match what's actually being evaluated — always check the actual expression being printed, not just its string label.
Mistake #3: Assuming an un-parameterized instance behaves unpredictably
Declaring an object with no #() at all, like plain something sth3;, simply uses the parameter's own declared default value. Why? This works exactly like any other default function argument — there's nothing special or unpredictable about omitting the override.
Mistake #4: Confusing a value parameter with a type parameter
Both use the same #(...) syntax at instantiation, but they override fundamentally different things — one overrides a numeric value (like size), the other overrides an entire data type (like T). Why? Mixing them up — like trying to pass a type where a plain value parameter is expected — produces a compile error, since the class declaration itself fixes which kind of parameter each one is.
Practice Exercise
Challenge: A Parameterized Fixed-Size Buffer
Write a class Buffer #(type T = int, int depth = 4) with a property data declared as an array of depth elements of type T (T data[depth];). Instantiate it twice: once with the defaults, and once overriding both T to byte and depth to 2. Print $bits(data) for each.
Hint
A class can declare more than one parameter, separated by commas, mixing value parameters and type parameters freely — override either, both, or neither at instantiation.
Solution
class Buffer #(type T = int, int depth = 4);
T data [depth];
endclass
module tb;
Buffer b1; // defaults: int, depth=4
Buffer #(byte, 2) b2; // byte, depth=2
initial begin
b1 = new;
b2 = new;
$display ("b1 data = %0d bits", $bits(b1.data));
$display ("b2 data = %0d bits", $bits(b2.data));
end
endmodule
b1 uses the defaults: 4 elements of int (32 bits each) = 128 bits total. b2 overrides both parameters: 2 elements of byte (8 bits each) = 16 bits total — both parameters, value and type, are overridden together in a single #(...) list.
Summary
In this tutorial, you learned:
- A parameterized class avoids rewriting nearly-identical code for different sizes or types, using the same
#()syntax already familiar from parameterized modules - A class parameter can be overridden positionally or by name, and defaults to whatever value the class declares if left unspecified
- A parameter can control a plain value (like a bit-vector width) or an entire data type, using the
typekeyword - Assigning a value that doesn't fit into a parameterized width truncates silently, rather than raising an error
typedefcan give a specific parameterization of a class its own convenient name for repeated use
The super keyword is used from within a sub-class to refer to properties and methods of the base class. It's mandatory to use super to reach a base class's own version of a property or method, once the sub-class has overridden it with its own version of the same name.
7 min read | Intermediate Level
What You'll Learn
- What
superrefers to, and exactly when you're required to use it - Why
superonly works inside a class that actually extends another - How
super.method_name()reaches the base class's own version of an overridden method - How this ties into the constructor-chaining pattern from SystemVerilog Class Constructor
super Requires an Actual Inheritance Relationship
super can only be used within a class scope that actually derives from a base class. The code below has a compile error, because extPacket is missing the extends keyword — it isn't really a child of Packet at all, despite the name:
class Packet;
int addr;
function display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
class extPacket; // "extends" keyword missing -> not a child class
function new ();
super.new ();
endfunction
endclass
module tb;
Packet p;
extPacket ep;
initial begin
ep = new();
p = new();
p.display();
end
endmodule
Output
super.new ();
|
ncvlog: *E,CLSSPX (testbench.sv,12|8): 'super' can only be used within a class scope that derives from a base class.
Adding extends Packet fixes this:
class extPacket extends Packet; // extPacket is a child class of Packet
function new ();
super.new ();
endfunction
endclass
Output
ncsim> run [Base] addr=0x0 ncsim: *W,RNQUIE: Simulation is complete.
The output here comes from p.display() — a plain Packet object, unrelated to extPacket. This example only demonstrates that the compile error is gone; extPacket itself isn't actually exercised yet. The next section puts it to real use.
Quick Check
Q: If extPacket kept super.new() inside its constructor but the extends Packet clause were removed again, would that be a compile-time or a run-time error?
Show Answer
Compile-time — exactly like the first example. super requires an actual inheritance relationship to exist at all, and that's checked when the code is compiled, not when it runs.
Accessing an Overridden Base Method With super
Here, extPacket's own display() calls Packet's version explicitly with super.display(), before adding its own line:
class Packet;
int addr;
function display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
class extPacket extends Packet;
function display();
super.display(); // Call base class display method
$display ("[Child] addr=0x%0h", addr);
endfunction
function new ();
super.new ();
endfunction
endclass
module tb;
Packet p;
extPacket ep;
initial begin
ep = new();
p = new();
ep.display();
end
endmodule
Output
ncsim> run [Base] addr=0x0 [Child] addr=0x0 ncsim: *W,RNQUIE: Simulation is complete.
ep.display() now runs both lines — the base class's own output first (via super.display()), followed by the child's own line. Without super, extPacket's override would have completely replaced Packet's behavior instead of building on it.
Quick Check
Q: If extPacket's display() didn't call super.display() at all, what would ep.display() print?
Show Answer
Just [Child] addr=0x0 — only the child's own line. super.display() is what explicitly pulls in the base class's output; without it, an override runs only its own statements.
Where Is This Used?
UVM component phase methods follow exactly this pattern — an overridden run_phase(phase) or build_phase(phase) commonly calls super.run_phase(phase) or super.build_phase(phase) at the very start, ensuring the base class's own setup still happens before the derived class's additional logic runs, rather than silently skipping it.
Common Beginner Mistakes
Mistake #1: Using super in a class that doesn't actually extend anything
As the first example shows, this is a compile-time error, not a warning. Why? super only has meaning relative to an actual base class — a missing extends clause (even with a name that looks like it implies inheritance, like extPacket) means there's simply nothing for super to refer to.
Mistake #2: Using super for a method the child never actually overrides
If a child class doesn't redefine a method at all, calling it unqualified already reaches the base class's version — no super needed. Why? super is specifically for reaching the base class's version of something the child has overridden with its own version of the same name; using it elsewhere is unnecessary, though not harmful.
Mistake #3: Assuming super automatically chains through every ancestor
super.method_name() only calls exactly what's written at that one level — it isn't an automatic "run everything from every ancestor" mechanism. Why? If a three-level inheritance chain needs every level's behavior to run, each level's own override has to explicitly call super.method_name() itself; skip one level's call, and the chain stops there.
Practice Exercise
Challenge: Extend the Chain One More Level
Add a third class, extPacket2, extending extPacket, with its own display() that calls super.display() first, then prints its own line. What does calling display() on an extPacket2 object print?
Hint
Each level's super.display() call reaches exactly one level up — work out what each level in the chain actually calls before combining them.
Solution
class extPacket2 extends extPacket;
function new ();
super.new ();
endfunction
function display ();
super.display ();
$display ("[Grandchild] addr=0x%0h", addr);
endfunction
endclass
module tb;
extPacket2 gc;
initial begin
gc = new ();
gc.display ();
end
endmodule
This prints three lines: [Base] addr=0x0, then [Child] addr=0x0, then [Grandchild] addr=0x0 — extPacket2's super.display() reaches extPacket's override, which itself calls super.display() to reach Packet's original version, chaining all the way back.
Summary
In this tutorial, you learned:
superrefers to the base class from within a sub-class, and is required to reach a base class's version of a property or method the sub-class has overriddensuperonly works inside a class that genuinely extends another — using it anywhere else is a compile-time errorsuper.method_name()calls exactly the base class's own version of that method, letting an override extend behavior instead of fully replacing itsuperdoesn't automatically chain through every level of a multi-level inheritance hierarchy — each level needs its own explicit call for the chain to continue
In Inheritance and Polymorphism, we saw that a method called through a base class handle pointing to a child class instance still ends up executing the base class's own version, not the child's. If that method is declared virtual, only then does the child class's version actually run:
bc = sc; // Base class handle is pointed to a sub class
bc.display (); // This calls the display() in base class and
// not the sub class as we might think
This article puts both versions side by side, using the exact same classes from those two articles, so the effect of adding virtual is completely isolated — nothing else about the code changes.
7 min read | Intermediate Level
What You'll Learn
- Why a non-virtual method dispatches based on the handle's declared type, not the object's actual type
- How declaring a method
virtualchanges dispatch to the object's actual type instead - Whether a child class's override needs to repeat the
virtualkeyword itself - The general rule of thumb for when a base class method should be declared
virtual
Without virtual
// Without declaring display() as virtual
class Packet;
int addr;
function new (int addr);
this.addr = addr;
endfunction
// This is a normal function definition which
// starts with the keyword "function"
function void display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
// ExtPacket from the Inheritance article, repeated here since this
// article's examples build on it directly
class ExtPacket extends Packet;
int data;
function new (int addr, data);
super.new (addr);
this.data = data;
endfunction
function void display ();
$display ("[Child] addr=0x%0h data=0x%0h", addr, data);
endfunction
endclass
module tb;
Packet bc;
ExtPacket sc;
initial begin
sc = new (32'hfeed_feed, 32'h1234_5678);
bc = sc;
bc.display ();
end
endmodule
Output
ncsim> run [Base] addr=0xfeedfeed ncsim: *W,RNQUIE: Simulation is complete.
The base class's own display() runs, even though bc actually points to an ExtPacket object — dispatch here is based on bc's declared type, Packet.
With virtual
class Packet;
int addr;
function new (int addr);
this.addr = addr;
endfunction
// Here the function is declared as "virtual"
// so that a different definition can be given
// in any child class
virtual function void display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
// ExtPacket from the Inheritance article, repeated here since this
// article's examples build on it directly
class ExtPacket extends Packet;
int data;
function new (int addr, data);
super.new (addr);
this.data = data;
endfunction
virtual function void display ();
$display ("[Child] addr=0x%0h data=0x%0h", addr, data);
endfunction
endclass
module tb;
Packet bc;
ExtPacket sc;
initial begin
sc = new (32'hfeed_feed, 32'h1234_5678);
bc = sc;
bc.display ();
end
endmodule
Output
ncsim> run [Child] addr=0xfeedfeed data=0x12345678 ncsim: *W,RNQUIE: Simulation is complete.
The exact same bc.display() call now runs ExtPacket's own version instead — with virtual, dispatch is based on the object's actual type, not the handle's declared type. The key takeaway: declare base class methods virtual whenever a child class might override them, so that existing base class handles correctly reach the override.
Quick Check
Q: In the "With virtual" example, does ExtPacket's own display() need the virtual keyword too, or only Packet's?
Show Answer
Only the base class's method strictly needs to be declared virtual for dispatch to work correctly — once a method is virtual anywhere in the hierarchy, every override of it behaves virtually too. That said, repeating virtual on the override (as shown here) is common practice for readability, so anyone reading the child class alone can immediately see it's participating in virtual dispatch.
Where Is This Used?
UVM's base classes declare nearly every method a user is expected to override — build_phase(), run_phase(), write(), and many others — as virtual, precisely so that user-extended components work correctly when the testbench handles them generically through their base class type, exactly the scenario demonstrated in this article.
Common Beginner Mistakes
Mistake #1: Forgetting to declare a base class method virtual
As the first example shows, this silently causes a base-typed handle to always run the base class's own version, no matter what child object it actually points to. Why? This can be a genuinely hard bug to spot, since the code compiles and runs without any error — it just quietly produces the base class's behavior instead of the override you expected.
Mistake #2: Assuming virtual needs to be repeated on every override
As the Quick Check above explains, only the base declaration strictly needs virtual. Why? It's not wrong to repeat it on each override — and it's often good practice for clarity — but functionally, the override participates in virtual dispatch either way, once the base method is virtual.
Mistake #3: Assuming virtual affects property access too
Declaring a method virtual has no effect on how class properties are accessed. Why? As shown in SystemVerilog Polymorphism, accessing a child-only property through a base-typed handle is a compile-time error regardless of whether any methods are virtual — virtual only changes method dispatch, not what members a handle's declared type can see.
Practice Exercise
Challenge: Make a Three-Level Chain Virtual
Using Packet, ExtPacket, and a third class ExtPacket2 (extending ExtPacket, adding a crc property and its own display()), declare display() as virtual at every level. Create an ExtPacket2 object, assign it to a Packet-typed handle, and call display() through that handle. Which version runs?
Hint
Virtual dispatch always resolves to the most-derived override that actually exists on the real object — it doesn't matter how many levels of inheritance separate the handle's declared type from the object's actual type.
Solution
class ExtPacket2 extends ExtPacket;
int crc;
function new (int addr, data, crc);
super.new (addr, data);
this.crc = crc;
endfunction
virtual function void display ();
$display ("[Grandchild] addr=0x%0h data=0x%0h crc=0x%0h", addr, data, crc);
endfunction
endclass
module tb;
Packet bc;
ExtPacket2 gc;
initial begin
gc = new (32'hfeed_feed, 32'h1234_5678, 32'habab_cdcd);
bc = gc; // bc is Packet-typed, but points to an ExtPacket2 object
bc.display ();
end
endmodule
ExtPacket2's own display() runs, printing the [Grandchild] version with all three fields — even though bc is declared as plain Packet, three inheritance levels away from the object's actual type. Virtual dispatch reaches all the way down to whichever override the real object actually has.
Summary
In this tutorial, you learned:
- Without
virtual, a method called through a handle always runs the version matching the handle's declared type, not the object's actual type - Declaring a base class method
virtualmakes dispatch depend on the object's actual type instead, correctly reaching a child class's override - Only the base declaration strictly needs
virtualfor this to work, though repeating it on overrides is common practice for readability virtualonly affects method dispatch — it has no effect on how class properties are accessed through a handle- The general rule: declare a base class method
virtualwhenever a child class might reasonably want to override it
Inheritance is a concept in OOP that lets you extend a class to create another class, gaining access to all the properties and methods of the original parent class through the new class's own object handle. The idea is to let you add new properties and methods into the new class while still keeping access to the original class members — and to make modifications without touching the base class at all.
8 min read | Intermediate Level
What You'll Learn
- How
extendsbuilds a child class from a parent, inheriting its properties and methods - How a child class can override a parent's method with its own version
- Why calling an overridden method through a parent-typed handle uses the parent's version, not the child's, unless the method is declared
virtual - How to call a parent's own version of an overridden method from inside the child, using
super
A Basic Example
ExtPacket is extended from, and is therefore a child class of, Packet. Being a child class, it inherits properties and methods from its parent. Both classes define a method called display() — when called through a child class handle, the child's own version runs; when called through a parent class handle, the parent's version runs:
class Packet;
int addr;
function new (int addr);
this.addr = addr;
endfunction
function display ();
$display ("[Base] addr=0x%0h", addr);
endfunction
endclass
// A subclass called 'ExtPacket' is derived from the base class 'Packet' using
// 'extends' keyword which makes 'ExtPacket' a child of the parent class 'Packet'
// The child class inherits all variables and methods from the parent class
class ExtPacket extends Packet;
// This is a new variable only available in child class
int data;
function new (int addr, data);
super.new (addr); // Calls 'new' method of parent class
this.data = data;
endfunction
function display ();
$display ("[Child] addr=0x%0h data=0x%0h", addr, data);
endfunction
endclass
module tb;
Packet bc; // bc stands for BaseClass
ExtPacket sc; // sc stands for SubClass
initial begin
bc = new (32'hface_cafe);
bc.display ();
sc = new (32'hfeed_feed, 32'h1234_5678);
sc.display ();
end
endmodule
Output
ncsim> run [Base] addr=0xfacecafe [Child] addr=0xfeedfeed data=0x12345678 ncsim: *W,RNQUIE: Simulation is complete.
sc's constructor calls super.new(addr) to run Packet's own constructor first (setting addr), then sets its own data property — exactly the constructor-chaining pattern covered in SystemVerilog Class Constructor. ExtPacket's own display() completely replaces Packet's version rather than adding to it, which is why bc and sc print in different formats.
Quick Check
Q: If ExtPacket didn't define its own display() at all, what would sc.display() print?
Show Answer
[Base] addr=0xfeedfeed — with no override, ExtPacket simply inherits Packet's display() unchanged, and that version has no idea data even exists, so it would only print addr.
Why the Handle's Type Matters (Without virtual)
It gets more interesting once a base-class-typed handle ends up pointing to a child class object — something that's perfectly legal, since a child class object is also a valid kind of its parent class:
module tb;
Packet p;
ExtPacket sc;
initial begin
sc = new (32'hfeed_feed, 32'h1234_5678);
// p is declared as type Packet, but now points to the SAME
// ExtPacket object that sc points to
p = sc;
p.display ();
end
endmodule
Since neither display() here is declared virtual, p.display() is resolved based on p's declared type (Packet), not the actual object it points to (an ExtPacket). The result: this prints [Base] addr=0xfeedfeed — Packet's version of display(), even though p is actually pointing at the exact same object as sc, which has a data field this call never shows.
This is exactly the kind of surprising behavior virtual methods exist to fix — with virtual, the call would instead dispatch based on the object's actual type, not the handle's declared type. See SystemVerilog Virtual Methods and SystemVerilog Polymorphism for the full picture.
Accessing the Parent's Version With super
Overriding a method doesn't have to mean completely replacing the parent's behavior — from inside the child class, super.method_name() calls the parent's own version explicitly, the same way super.new() calls the parent's constructor:
class ExtPacket extends Packet;
int data;
function new (int addr, data);
super.new (addr);
this.data = data;
endfunction
function display ();
// Run the parent's own display() first, then add the child's own line
super.display ();
$display (" data=0x%0h", data);
endfunction
endclass
Calling sc.display() with this version would print both lines — Packet's own "[Base] addr=..." line first (via super.display()), followed by the child's own "data=..." line — instead of the child's version having to duplicate the parent's formatting itself.
Where Is Inheritance Used?
UVM is built almost entirely on inheritance — uvm_driver, uvm_monitor, and every other component base class exist so that a project's own driver, monitor, and scoreboard classes can extend them, inheriting shared infrastructure while overriding only the parts that are actually specific to that test.
Common Beginner Mistakes
Mistake #1: Assuming a base-typed handle always calls the "most derived" method
As the second example shows, calling a method through a base-class-typed handle uses the base class's own version, even if the handle actually points to a child object. Why? This only changes once the method is declared virtual — without it, dispatch is based on the handle's declared type, not the object's actual type.
Mistake #2: Forgetting super.new() when the base constructor requires arguments
If the parent's constructor requires an argument with no default, the child's constructor must explicitly call super.new(...) with it. Why? SystemVerilog can only call the base constructor implicitly if it takes no required arguments — otherwise, omitting the explicit call is a compile-time error.
Mistake #3: Fully overriding a method when you meant to extend it
A child class's override completely replaces the parent's version unless the child explicitly calls super.method_name() somewhere inside it. Why? It's easy to rewrite a method from scratch in the child class and forget that the parent's own version is still available — often what you actually want is to run the parent's version and then add to it.
Mistake #4: Redeclaring inherited properties in the child class
A child class automatically has access to every property and method its parent declares — it never needs to redeclare them. Why? Only genuinely new properties, like data in ExtPacket, need their own declaration in the child; everything inherited is already there.
Practice Exercise
Challenge: Chain super.display() Across Three Levels
Extend ExtPacket with a third class, ExtPacket2, adding its own property crc. Give it a display() that calls super.display() first, then prints crc. What gets printed when you call display() on an ExtPacket2 object?
Hint
Each level's super.display() call triggers the level above it, which (if it also uses super.display()) triggers the level above that — the calls chain all the way back to Packet.
Solution
class ExtPacket2 extends ExtPacket;
int crc;
function new (int addr, data, crc);
super.new (addr, data);
this.crc = crc;
endfunction
function display ();
super.display ();
$display (" crc=0x%0h", crc);
endfunction
endclass
Calling display() on an ExtPacket2 object prints three lines: Packet's "[Base] addr=..." line, ExtPacket's "data=..." line, and finally ExtPacket2's own "crc=..." line — each level's super.display() call reaches one level further back, all the way up the chain.
Summary
In this tutorial, you learned:
extendscreates a child class that inherits all properties and methods from its parent, without modifying the parent itself- A child class can override a parent's method with its own version
- Without
virtual, calling a method through a handle uses whatever version matches the handle's declared type, not the actual object's type super.method_name()calls the parent's own version of a method from inside a child class's override, letting you extend behavior instead of fully replacing it- A child class only needs to declare properties and methods that are genuinely new — everything inherited is already accessible
Quiz
No quiz questions available for this article.