Inheritance and Polymorphism
Learn how SystemVerilog classes can be extended and used through a common interface—the foundation of reusable UVM code.
Learning objectives
- Explain what inheritance means in SystemVerilog.
- Create a derived class using
extends. - Understand what members are inherited from a base class.
- Override a method in a derived class.
- Understand why
virtualmethods are important. - Explain polymorphism using a practical verification example.
- Understand why inheritance and polymorphism are important in UVM.
Step 1: What is inheritance?
Inheritance allows one class to reuse and extend another class.
Instead of creating a completely new class from scratch, we can create a new class based on an existing class.
For example, suppose we have a general packet:
class packet;
bit [7:0] addr;
bit [31:0] data;
endclass
We can create another class based on packet instead of
redefining addr and data.
class write_packet extends packet;
endclass
The write_packet class is called the
derived class.
The packet class is called the
base class.
Step 2: The extends keyword
SystemVerilog uses the extends keyword to create
inheritance.
class write_packet extends packet;
endclass
This means:
write_packet
|
| inherits from
v
packet
The derived class can use the properties and methods available through the base class.
Example
class packet;
bit [7:0] addr;
bit [31:0] data;
endclass
class write_packet extends packet;
bit [3:0] byte_enable;
endclass
A write_packet contains:
addrfrompacketdatafrompacketbyte_enabledefined bywrite_packet
The derived class can therefore reuse existing functionality while adding new functionality.
Step 3: Why inheritance is useful
Imagine a verification environment that works with several types of transactions.
They may all share common information such as:
- Address
- Data
- Transaction ID
Instead of defining those properties repeatedly, we can put the common information into a base class.
class bus_transaction;
bit [7:0] addr;
bit [31:0] data;
endclass
Then specialized transactions can extend it.
class read_transaction extends bus_transaction;
endclass
class write_transaction extends bus_transaction;
bit [3:0] byte_enable;
endclass
This gives us a hierarchy:
bus_transaction
|
+---------+---------+
| |
v v
read_transaction write_transaction
Step 4: Inherited methods
Inheritance applies to methods as well as properties.
class packet;
bit [7:0] addr;
bit [31:0] data;
function void print();
$display("addr=%h data=%h", addr, data);
endfunction
endclass
A derived class can use the inherited print() method.
class write_packet extends packet;
bit [3:0] byte_enable;
endclass
write_packet pkt;
initial begin
pkt = new();
pkt.addr = 8'h10;
pkt.data = 32'h1234_5678;
pkt.byte_enable = 4'b1111;
pkt.print();
end
We did not have to rewrite print() in
write_packet.
The derived class inherited it from packet.
Step 5: Adding specialized behavior
A derived class can add its own properties and methods.
class write_packet extends packet;
bit [3:0] byte_enable;
function void print_write_info();
$display("byte_enable=%b", byte_enable);
endfunction
endclass
Now a write_packet has both the functionality inherited
from packet and its own specialized functionality.
write_packet pkt;
initial begin
pkt = new();
pkt.addr = 8'h20;
pkt.data = 32'hABCD_1234;
pkt.byte_enable = 4'b1010;
pkt.print();
pkt.print_write_info();
end
Step 6: Method overriding
Sometimes a derived class needs to provide a different implementation of a method inherited from the base class.
This is called method overriding.
class packet;
function void print();
$display("Generic packet");
endfunction
endclass
class write_packet extends packet;
function void print();
$display("Write packet");
endfunction
endclass
Both classes have a method called print(), but they
provide different behavior.
Step 7: Why virtual matters
To understand polymorphism correctly, we need to understand
virtual methods.
Make the base-class method virtual:
class packet;
virtual function void print();
$display("Generic packet");
endfunction
endclass
The derived class can override it:
class write_packet extends packet;
function void print();
$display("Write packet");
endfunction
endclass
Now SystemVerilog can select the implementation based on the actual object being referenced.
Step 8: What is polymorphism?
Polymorphism means that one common interface can be used with objects of different derived types.
This sounds complicated, but the basic idea is simple.
A base-class handle can refer to an object of a derived class.
packet pkt;
pkt = new write_packet();
Here:
pktis a handle of typepacket.- The actual object is a
write_packet.
pkt
|
| refers to
v
+----------------------+
| write_packet object |
| ------------------------------------- |
| packet properties |
| write properties |
| +----------------------+ |
This is one of the most important ideas behind polymorphism.
Step 9: Polymorphism in action
Let's put everything together.
class packet;
virtual function void print();
$display("Generic packet");
endfunction
endclass
class write_packet extends packet;
function void print();
$display("Write packet");
endfunction
endclass
class read_packet extends packet;
function void print();
$display("Read packet");
endfunction
endclass
Now create a base-class handle:
packet pkt;
initial begin
pkt = new write_packet();
pkt.print();
pkt = new read_packet();
pkt.print();
end
The output will be:
Write packet
Read packet
Notice something important:
The variable pkt always has type packet,
but it refers to different derived objects.
The overridden print() method is selected according
to the actual object.
Step 10: Why polymorphism matters in verification
Verification environments often need to work with different types of transactions through a common interface.
For example:
bus_transaction tr;
The environment can work with the common bus_transaction
type while different derived transaction types provide specialized
behavior.
class bus_transaction;
virtual function void print();
$display("Generic bus transaction");
endfunction
endclass
class read_transaction extends bus_transaction;
function void print();
$display("READ transaction");
endfunction
endclass
class write_transaction extends bus_transaction;
function void print();
$display("WRITE transaction");
endfunction
endclass
A component can use the base type:
function void show_transaction(bus_transaction tr);
tr.print();
endfunction
It does not need to know whether the object is a read or write transaction.
Step 11: Inheritance and polymorphism in UVM
UVM uses inheritance everywhere.
Later in this course you will see code such as:
class my_transaction extends uvm_sequence_item;
endclass
Your class is derived from a UVM base class.
You will also see:
class my_driver extends uvm_driver;
endclass
class my_monitor extends uvm_monitor;
endclass
class my_test extends uvm_test;
endclass
Each class extends an existing UVM class and specializes it for your verification environment.
extends in UVM, you are using the
SystemVerilog inheritance mechanism you learned in this lesson.
Later, UVM's factory and other mechanisms will make heavy use of polymorphism. You will learn those concepts in the UVM Foundations section.
Step 12: A simple class hierarchy
Here is a useful mental model for what you have learned:
Base Class
|
v
bus_transaction
|
+--------+--------+
| |
v v
read_transaction write_transaction
| |
+--------+--------+
|
Base handle
|
v
bus_transaction tr;
The important idea is that the code using
bus_transaction does not necessarily need to know
the exact derived class.
That flexibility is one of the reasons object-oriented programming is so useful for reusable verification environments.
Inheritance vs. polymorphism
| Concept | What it does | SystemVerilog example |
|---|---|---|
| Inheritance | Creates a new class based on an existing class | class child extends parent; |
| Derived class | Specializes or extends a base class | class write_packet extends packet; |
| Method overriding | Provides a specialized implementation | function void print(); |
| Virtual method | Allows derived behavior through a base handle | virtual function void print(); |
| Polymorphism | Uses a common base type with different derived objects | packet p = new write_packet(); |
Try it yourself
Create a base class called transaction.
Give it:
- An
addrproperty - A
dataproperty - A virtual
print()method
Then create two derived classes:
read_transactionwrite_transaction
Override print() in each derived class so that they
print different messages.
Finally, create a base-class handle:
transaction tr;
Make it refer to a read transaction and call print().
Then make it refer to a write transaction and call print().
tr, can call different
implementations of print().
Quick knowledge check
Question 1
Which keyword is used to create an inherited class?
inheritextendssuperparent
extends creates an inheritance relationship.
Question 2
What is the main purpose of inheritance?
- To generate random numbers
- To reuse and extend existing class functionality
- To create clock signals
- To connect a DUT interface
Question 3
What does this declaration mean?
packet pkt;
pkt = new write_packet();
pktis a write_packet handlepktis a packet handle referring to a write_packet object- The write_packet class is deleted
- A random packet is generated
Question 4
What is polymorphism?
- Using random constraints
- Using a base type with objects of different derived types
- Creating multiple clocks
- Copying an object
Question 5
Why are virtual methods important for polymorphism?
- They enable derived implementations to be selected through a base handle.
- They create new objects automatically.
- They randomize properties.
- They connect UVM ports.
Common beginner mistakes
extends creates a class relationship.
It does not create an object.
packet pkt;
pkt = new write_packet();
The handle is a packet handle, but the actual
object is a write_packet.
virtual when relying on
polymorphic behavior
Virtual methods are an important part of runtime polymorphism. Make the base-class method virtual when derived classes need to override its behavior.
If several related classes share the same data or behavior, consider putting the common functionality in a base class.
Key takeaways
- Inheritance allows one class to reuse and extend another class.
- The
extendskeyword creates an inheritance relationship. - The original class is the base class.
- The new class is the derived class.
- A derived class can inherit properties and methods from its base class.
- A derived class can add new properties and methods.
- A derived class can override a base-class method.
- A
virtualmethod enables derived behavior through a base-class handle. - Polymorphism allows one base type to work with different derived objects.
- UVM uses inheritance and polymorphism throughout its class hierarchy.
What's next?
You now know how classes can be extended and how polymorphism allows different objects to be handled through a common base type.
Next, we will use another powerful SystemVerilog feature for verification: randomization.
In the next lesson, you will learn how SystemVerilog can automatically generate different values for your transaction objects instead of manually creating every test scenario.