[Edit of Image1]
Hey it's a me again @drifter1!
Today we continue with the Logic Design series on SystemVerilog in order to cover more about Classes. This is part 2 and so I highly suggest checking out part 1 beforehand.
So, without further ado, let's dive straight into it!
Last time we showed how multiple objects can point to the same instance:
className i1, i2;
i1 = new(); // object creation
i2 = i1; // point to same instance
Copying an object can be done using the keyword new followed by the corresponding instance name.
className i3;
i3 = new i2;
But, be careful, as this approach (a "shallow" copy) doesn't take care of nested objects. For nested objects we have to make a "deeper" copy by writing a custom task, let's say copy(), and copying any necessary data over in that task.
class className;
nestedObject no;
task copy(ref className other);
begin
other = new this;
other.no = new this.no;
...
end
endtask
endclass
Of course, this task is called on the to-be-copied object, and the target is put as a parameter!
className i4;
i1.copy(i4);
Being OOP, SystemVerilog also offers Inheritance, which is the concept of OOP that allows a class to extend another class. That way, properties and methods from the parent (or super) class can be called from the child (or sub) class.
Inheriting from a class is done by adding the extends keyword followed by the super class's name in the class declaration.
class className extends superClass;
...
endclass
Methods from the parent class can of course be overridden by adding a new definition for them in the sub class.
From within the inheriting class (not anywhere else!) its possible to access the parent class's original properties and methods using the super keyword. That way the methods of the class can call the original methods.
For example, a print() task could call the original print() together with new code.
task print()
begin
...
super.print();
...
end
endtask
SystemVerilog allows us to assign a subclass to the superclass's handler. This is known as Polymorphism.
For example:
parentClass pc;
subClass sc;
sc = new();
pc = sc;
Even though pc points to sc, the original (parent class) methods and not the overridden methods will be called instead!
Assigning the parent class's handler to the subclass's handler is not possible and will yield a compilation or runtime error.
sc = pc; // not possible
Continuing on from the previous topic of Polymorphism, what if we want to invoke the child class method instead of the parent class method? Well, simply mark the corresponding methods as virtual, which basically states that different definitions can be given by any child class.
So, what the following code executes depends on if the example() method was marked as virtual in the parent class.
pc = sc;
pc.example();
example() methodvirtual keyword : subclass's example() methodBlock diagrams and other visualizations were made using draw.io
And this is actually it for today's post!
Next time we will continue on with more on Classes...
See Ya!

Keep on drifting!