In SystemVerilog, this
and super
are special object handles used inside class-based code.
They help when:
- You need to refer to current or parent class members
- You’re working with inheritance
- You want to clearly specify which variable or method you’re referring to
What is this
? #
The this
keyword is a handle that refers to the current object of the class.
Purpose of this
#
- To differentiate between class variables and local variables (e.g., method arguments with the same name)
- To refer to the current object explicitly
- To pass the current object to functions, modules, or other objects
Example: Disambiguating variable names
class Packet;
bit [7:0] addr;
function void set_addr(bit [7:0] addr);
this.addr = addr; // 'this.addr' is the class variable,right-side is argument
endfunction
endclass
Without this
keyword, addr = addr;
assigns the argument to itself — which is incorrect!
What is super
? #
In SystemVerilog, when a child class overrides a method from its parent class, the original version in the parent gets hidden. However, if you still want to access or invoke the parent’s original method from the child class, you can use the super
keyword.
Syntax:
super.method_name();
Why Use super
? #
- To extend the behavior of a method instead of completely replacing it.
- Useful when the child needs to add some functionality before or after the parent’s logic.
Example:
class Parent;
function void display();
$display("This is Parent class display method");
endfunction
endclass
class Child extends Parent;
function void display();
$display("This is Child class display method");
// Call the parent's version
super.display();
endfunction
endclass
module test;
initial begin
Child c = new();
c.display();
end
endmodule
Output:
This is Child class display method
This is Parent class display method
Comparison Table: this
vs super
#
Feature | this | super |
---|---|---|
Refers to | Current class object | Parent class object |
Use case | Access current class members | Access parent class members |
Constructor use | Cannot call constructor | Can call parent constructor (super.new() ) |
Scope | Inside the class | Inside inherited (child) class |
Overriding | Refers to overridden method/var | Accesses original version from parent |
Key Points #
- Use
this
to clearly refer to class members or pass the current object. - Use
super
when using inheritance to call or reuse parent class behavior. super
is especially useful when the child class overrides a function but still wants to use the parent version.this
is helpful for clarity, especially when variable names overlap.