SystemVerilog OOP Mastery · All levels

`super`, `super.new`, and Overriding Rules: Code Examples

Code Examples for `super`, `super.new`, and Overriding Rules.

Code examples

Constructor chaining with super.new in derived class

systemverilog
class base_cfg;
  string name;

  function new(string name = "base_cfg");
    this.name = name;
  endfunction
endclass

class axi_cfg extends base_cfg;
  int unsigned outstanding;

  function new(string name = "axi_cfg", int unsigned outstanding = 4);
    super.new(name);
    this.outstanding = outstanding;
  endfunction
endclass

The child constructor augments initialization but does not replace parent responsibilities; super.new keeps the base contract intact.

UVM build_phase override preserving parent behavior

systemverilog
class env_base extends uvm_env;
  `uvm_component_utils(env_base)

  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction

  virtual function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    `uvm_info(get_type_name(), "env_base build", UVM_LOW)
  endfunction
endclass

class soc_env extends env_base;
  `uvm_component_utils(soc_env)

  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction

  virtual function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    `uvm_info(get_type_name(), "soc_env build", UVM_LOW)
  endfunction
endclass

Calling super.build_phase keeps inherited setup while adding child behavior. Missing this call is a classic source of null handles and lost connections.

Related topics