Tutorials

Learn More

The super keyword is used in a child or derived class to refer to class members of its immediate base class.

Syntax:

super.<base_class_member>;  // It has to be used in derived class

Parent class members are overridden by its derived class on access by child class handle if both class members have the same name for class properties and methods. To refer to base class members in such a context, the super keyword is useful. To understand the usage of the super keyword, let me explain with two examples. The first example does not use a super keyword while the second example uses a super keyword.

In the below examples, 

  1. The data variable is a class property of both base and derived class.
  2. The display() method is also used in both classes.

Example without super keyword

class parent_trans;
  bit [31:0] data;
  
  function void display();
     $display("Base: Value of data = %0h", data);
  endfunction
endclass

class child_trans extends parent_trans;
  bit [31:0] data;
  
  function void display();
    $display("Child: Value of data = %0h", data);
  endfunction
endclass

module class_example;
  initial begin
    child_trans c_tr;
    c_tr = new();
    
    c_tr.data = 5; // data variable in base class will have default value as 0.
    c_tr.display();
  end
endmodule

Output:

Child: Value of data = 5

Example with super keyword

class parent_trans;
  bit [31:0] data;
  
  function void display();
     $display("Base: Value of data = %0h", data);
  endfunction
endclass

class child_trans extends parent_trans;
  bit [31:0] data;
  
  function void display();
    super.data = 3;
    super.display();
    $display("Child: Value of data = %0h", data);
  endfunction
endclass

module class_example;
  initial begin
    child_trans c_tr;
    c_tr = new();
    
    c_tr.data = 5;
    c_tr.display();
  end
endmodule

Output:

Base: Value of data = 3
Child: Value of data = 5

In the first example, the value for the data variables in the child class is updated. The display method from the child class is called using the child class handle.

Similarly, in the second example, the display() method from child class is called using child class handle, but the super keyword makes a difference. The super.data assignment assigns value to the data variable of the base class. Also, super.display() method calls display() method of its base class.

In the case of multilevel inheritance, class members are inherited one level up. The super.super.<class_member> is not allowed.

Usage of the super keyword in the constructor

By default, the SystemVerilog compiler calls super.new() function calls automatically from extended class. But if there are any arguments used in the new() function call, then the user has to call super.new(<argument_list>) explicitly. The super.new() function call shall be the first line in the derived class constructor because the parent class must be initialized before the derived class.

Example with arguments in the constructor

class parent_trans;
  bit [31:0] data;
  
  function new(bit [31:0] data);
    this.data = data;
    $display("Base: Value of data = %0h", data);
  endfunction
endclass

class child_trans extends parent_trans;
  bit [31:0] data;
  
  function new(bit [31:0] data_p, data_c);
    super.new(data_p);
    this.data = data_c;
    $display("Child: Value of data = %0h", data);
  endfunction

endclass

module class_example;
  initial begin
    child_trans c_tr;
    c_tr = new(5, 7);
  end
endmodule

Output:

Base: Value of data = 5
Child: Value of data = 7

Example without arguments in the constructor

This example is a little tricky. Usually, we create an object for both base and child class and then do a method call and assign values to their class properties. To prove that SystemVerilog does super.new() function calls internally. An object is only created for the child class and then the child class handle is assigned to its base class. This is required to refer to the memory location for a base class that is created on super.new() call. Later base class data variable value is assigned. On calling display() method for the corresponding class handle, to print respective class properties.

class parent_trans;
  bit [31:0] data;
  int id;
  
  function void display();
    $display("Base: Value of data = %0d, id = %0d", data, id);
  endfunction
endclass

class child_trans extends parent_trans;
  bit [31:0] data;
  
  function void display();
    $display("Child: Value of data = %0d, id = %0d", data, id);
  endfunction
endclass

module class_example;
  initial begin
    parent_trans p_tr;
    child_trans c_tr;
    c_tr = new();
    c_tr.data = 5;
    c_tr.id   = 2;
    
    p_tr = c_tr;
    
    p_tr.data = 10;
    p_tr.id   = 1;
    
    p_tr.display();
    c_tr.display();
  end
endmodule

Output:

Base: Value of data = 10, id = 1
Child: Value of data = 5, id = 1

Notice that the data variable has different values for base and child class since they are declared in both classes.

System Verilog Tutorials