The super keyword in SystemVerilog is a powerful feature of object-oriented programming (OOP) that allows derived classes to access properties and methods of their parent (or base) class. It provides a mechanism to enhance code reusability and maintainability by facilitating clear and organized inheritance structures.
This article explores the super keyword in SystemVerilog with detailed explanations and examples to help you integrate it effectively into your design verification projects.
What is the super Keyword?
In SystemVerilog, the super keyword is used within a derived class to:
- Call the constructor of the parent class explicitly.
- Access overridden methods or variables in the parent class.
By using super, you can extend or modify the behavior of inherited methods and ensure the parent class’s logic is executed.
Using super in Constructors
When creating a derived class, its constructor does not automatically call the constructor of the parent class. However, you can explicitly call it using the super keyword. This is particularly useful for initializing variables declared in the parent class.
Example: Constructor Inheritance
class ParentClass;
int id;
// Parent class constructor
function new(int id);
this.id = id;
$display("ParentClass Constructor: ID = %0d", id);
endfunction
endclass
class ChildClass extends ParentClass;
string name;
// Child class constructor
function new(int id, string name);
super.new(id); // Call parent class constructor
this.name = name;
$display("ChildClass Constructor: Name = %s", name);
endfunction
endclass
// Testbench to demonstrate super
module test;
initial begin
ChildClass obj = new(101, "TestChild");
end
endmodule
Output:
ParentClass Constructor: ID = 101
ChildClass Constructor: Name = TestChild
In this example, super.new(id) ensures that the ParentClass constructor runs before initializing variables specific to ChildClass. If we remove the super.new(id), it will throw an errow as it will be unable to access the parent class method.
Using super to Access Overridden Methods
When a method in the parent class is overridden in a child class, the super keyword allows you to invoke the parent class’s version of the method. This is useful when you want to augment the behavior of a method while retaining the original implementation.
Example: Overriding Methods
class ParentClass;
// Method in the parent class
function void display();
$display("This is the ParentClass method.");
endfunction
endclass
class ChildClass extends ParentClass;
// Override the display method
function void display();
super.display(); // Call the parent class's method
$display("This is the ChildClass method.");
endfunction
endclass
// Testbench to demonstrate method overriding
module test;
initial begin
ChildClass obj = new();
obj.display();
end
endmodule
Output:
This is the ParentClass method.
This is the ChildClass method.
In this example, super.display() ensures that the ParentClass implementation of display() is executed before adding custom behavior in ChildClass. If we run the above code removing super.display() in child class, it will only print child class display() method. The output will be:
This is the ChildClass method.
Key Points to Remember
- Use
super.new()to invoke the parent class constructor from the child class constructor. - Use
super.method_name()to call overridden methods in the parent class from a derived class. superhelps maintain clarity and correctness when extending parent class functionality.
