Skip to Objects + construction questions

Part 1 · Objects and construction

Separate types, objects, and handles, then initialize inherited and parameterized class state with explicit visibility and ownership.

Question directory

Questions for Objects + construction

Open an authored answer, then use the adjacent object diagram or code example to verify the mechanism.

  1. class model and visibilityWhat is the difference between a class, an object, and an object handle?SystemVerilog OOP
  2. class model and visibilityHow do encapsulation, abstraction, inheritance, and polymorphism differ?SystemVerilog OOP
  3. class model and visibilityHow do public, protected, and local visibility work in SystemVerilog classes?SystemVerilog OOP
  4. class model and visibilityDoes SystemVerilog support method overloading by argument type?SystemVerilog OOP
  5. class model and visibilityWhy keep class state protected or local behind methods?SystemVerilog OOP
  6. construction and specializationWhat does the new() constructor do in a SystemVerilog class?SystemVerilog OOP
  7. construction and specializationWhen must a derived constructor call super.new()?SystemVerilog OOP
  8. construction and specializationWhat is the difference between this and super?SystemVerilog OOP
  9. construction and specializationHow do value and type parameters specialize a class?SystemVerilog OOP
  10. construction and specializationWhat does static mean for a class property or method?SystemVerilog OOP

SystemVerilog class model and visibilityKnow what the handle actually owns.

Start with the SystemVerilog object model, the four OOP ideas, and the visibility rules that keep verification classes reusable without exposing mutable internals.

Concept model

A class defines behavior; handles reach objects

Type definitionOne class, many objects
blueprintclass Packet
  • addrproperty
  • dataproperty
  • format()method
handlepkt_aaddr = 10
handlepkt_baddr = 42
handlepkt_caddr = 91
Encapsulation boundaryVisibility follows the caller
unqualifiedclasschildoutside
protectedclasschildoutside
localclasschildoutside

The class is one type definition. Each new allocation creates a distinct object, while handle assignment can make multiple names refer to the same object.

Strong answer

A class is a type definition. An object is one allocated instance of that class. A class variable stores a handle that is either null or refers to an object; the handle is not the object itself.

Reason it through

  • Declaring Packet p creates a handle initialized to null. Executing p = new allocates a Packet object and places its handle in p.
  • Two different calls to new create two independent objects. Assigning one handle to another creates an alias to the same object.
  • Methods execute against the object reached through the handle, so a null-handle dereference is a runtime error.
Type, allocation, and aliasingsystemverilog
class Packet;
  int id;
endclass

Packet p;        // null handle
Packet alias_p;

p = new;         // allocate one Packet object
p.id = 7;
alias_p = p;     // same object, second handle
alias_p.id = 9;  // p.id is now 9

SystemVerilog construction and specializationInitialize the hierarchy in order.

Reason about new(), base-constructor chaining, this and super, parameterized class types, and state shared at class scope.

Concept model

Construction flows from base state to specialization

Constructor orderBuild the base before the child
01allocateDerived objectproperties receive defaults
02super.new(args)Base constructorestablish inherited state
03this.tag = tagDerived constructorfinish specialized state
Compile-time specializationOne template, explicit types
class templatePacket #(WIDTH, T)
WIDTH = 64value parameterT = bit[15:0]type parameter
specialized typePacket #(64, bit[15:0])

A derived constructor establishes inherited state before its own fields. Value and type parameters specialize the class at compile time rather than at object creation.