Module 3 — Object-Oriented Programming
The OOP foundation that makes UVM possible: classes, objects, constructors, inheritance, polymorphism, encapsulation, virtual methods, abstract classes, static members, and this/super — with testbench-flavored examples throughout.
📑 Table of Contents
26Class
A class is a user-defined type bundling data (properties) and behavior (methods) together — SystemVerilog's core OOP building block, and the foundation of every UVM component and transaction.
Every UVM transaction (uvm_sequence_item), driver, monitor, agent, and scoreboard is written as a SystemVerilog class. Understanding raw classes is a prerequisite for understanding any UVM code.
27Object
An object is a runtime instance of a class, created with the new() constructor call. Class variables hold a handle (reference) to the object, not the object itself.
Assigning one class variable to another (pkt2 = pkt1) copies the handle, not the object — both variables now point to the same underlying object in memory. This is functionally similar to reference/pointer semantics in C++/Java, and is a frequent source of confusing testbench bugs when engineers expect value-copy behavior.
28Constructor
The new() method is the constructor — automatically called when an object is created, used to initialize properties. Every class has an implicit default constructor unless one is explicitly defined.
If you don't explicitly call super.new() in a derived class's constructor, SystemVerilog implicitly calls the base class's zero-argument new() for you first. If the base constructor requires arguments, you must call super.new(...) explicitly, and it must be the first statement.
29Inheritance
Inheritance lets a class (derived/child) reuse and extend the properties and methods of another class (base/parent) via the extends keyword — the mechanism behind every UVM class hierarchy (e.g., your driver extends uvm_driver).
SystemVerilog supports only single inheritance — a class can extend just one base class. Multiple inheritance is achieved indirectly through interface classes (a class can implement several interface classes even while extending only one base class).
30Polymorphism
Polymorphism means a base-class handle can reference a derived-class object, and calling a (virtual) method through that handle invokes the derived class's overridden version — enabling generic code that works across a whole class family.
The UVM factory (uvm_factory) and sequencer/driver communication rely entirely on polymorphism: a driver's seq_item_port handles items through the base uvm_sequence_item type, but calls the correctly overridden methods of whatever derived transaction was actually generated.
31Encapsulation
Encapsulation restricts direct access to a class's internal data using access qualifiers, exposing controlled interaction through public methods instead — protecting invariants and hiding implementation details.
| Qualifier | Accessible From |
|---|---|
| (default, public) | Anywhere the handle is visible |
| protected | The declaring class and any classes that extend it |
| local | Only the declaring class itself (not even derived classes) |
A common interview trap: local members are not inherited-accessible — even a subclass cannot touch them directly. Use protected instead if derived classes need direct access.
32Virtual Methods
A virtual method enables true polymorphic dispatch — without it, calling an overridden method through a base-class handle would incorrectly run the base class's version instead of the derived one.
This exact base-handle/derived-object scenario is one of the most frequently asked SystemVerilog OOP interview questions. Without virtual, the method call resolves based on the static (declared) type of the handle; with virtual, it resolves based on the dynamic (actual object) type. Always mark methods virtual in a base class if you expect subclasses to override them.
33Abstract Classes
A virtual class (SystemVerilog's term for an abstract class) cannot be instantiated directly — it exists only to be extended, typically to enforce a common interface across all subclasses via pure virtual methods.
uvm_component and uvm_object rely heavily on this pattern — many UVM base classes declare pure virtual methods that your derived driver/monitor/scoreboard classes are required to implement (e.g., overriding run_phase).
34Static Members
static properties and methods belong to the class itself, not to any individual object — shared and visible across every instance, useful for counters, ID generators, or shared configuration.
Static methods can only directly access static properties — they have no implicit this handle since they aren't tied to any specific object, so they cannot touch non-static (instance) members without being given an explicit object handle.
35this and super
this refers to the current object instance (disambiguating member variables from same-named arguments); super refers to the immediate base-class portion of the current object, used to call overridden base-class methods or constructors.
| Keyword | Refers To | Common Use |
|---|---|---|
| this | The current object instance | Disambiguate member vs. local/argument names; pass self as an argument |
| super | The immediate base-class part of the current object | Call base-class constructor/method that was overridden |
If used, super.new(...) must be the first statement in a derived class's constructor — you cannot execute any other code before calling the base class constructor.
🎯 Module 3 Recap
- A
classbundles properties and methods; anobjectis a runtime instance accessed through a handle (reference), not stored by value. - The
new()constructor initializes an object; derived classes must properly chain tosuper.new(). extendsgives inheritance (single inheritance only); overriding +virtualgives true polymorphism.local/protectedqualifiers enforce encapsulation —localblocks even subclasses,protectedallows them.- Mark base-class methods
virtualwhenever subclasses might override them — otherwise base-handle calls silently use the wrong (base) implementation. virtual class+pure virtualmethods create abstract base types that enforce a common interface, exactly like UVM's own base classes.staticmembers are shared across all objects of a class; static methods can't touch non-static members without an explicit handle.this= current object;super= immediate base-class portion of the current object.
📚 Next up — Module 4: Randomization & Constraints (rand, randc, constraint blocks, randomize(), pre/post_randomize, and constraint solving order)
↑ Back to Table of Contents
Comments
Post a Comment