[Day-3] SystemVerilog Shenanigans: Unraveling Inheritance, Polymorphic Play, and Class Assignments

Eraz Ahmed
6 min readJul 9, 2024

--

Image collected from Google Image

Now that we have a working knowledge of class, handle, and constructor, we are going to go ahead and learn about how we can extend the possibility of getting members shared among different objects.

Doesn’t make sense right?

Let’s break it down a bit!

Say I inherited something from my father. What do you understand by it? In simple words, it means I got something from my father, that he owned at a certain time in his life.

Inheritance in OOP is also something like this. It means that we can pass a certain member, it can be a function or maybe a variable, from a class to another class.

To give you a perspective, let’s follow the example below:

class base;
string start;
function new();
start = "This is:";
endfunction
function display ();
$display("This is the end");
endfunction
endclass
class child extends base;
string full;
string name;
function new(string name);
super.new();
this.name = name;
full = {start," ",name};
endfunction
function display_sc ();
$display("%s",full);
endfunction
endclass
module tb;
child sc = new("inheritance");
initial begin
sc.printer();
sc.display();
end
endmodule

Here we have two classes. The first one is named base and the second one is child. If you look into the first class, you will notice it has two functions, one being the constructor function and the other one is a simple function that displays something. If you look into the variables, we have only one, naming start.

Let’s take a detour now!

If we want to assign a value to any variable, it is a good practice to do it inside of the constructor function. Why?

Let’s notice the following:

class exp;
string start = “This is an”;
string full = {start, end};
string end = “Experiment”;
endclass

If we did this, it would give an error saying the end is not defined. This means we need to define the start and end variables first before concatenating them in full variables.

On the other hand, this is completely normal and also foolproof.

class exp;
string start = “This is an”;
string full ;
string end = “Experiment”;
function new();
full = {start, end};
endfunction
endclass

Okay, let’s go back to inheritance now.

Now we understand why we put values to the start variable inside of a new function in the class base, the first class, defined in the example.

Let’s get onto the second class, child. We don’t have much in it apart from an expanded keyword and a function having a normal display function in it.

What expand does here is that it helps get members from one class to another. This means we can call the variables that belong to the parent class(base) in the child class(child). Just that is done in the constructor function of the child class and is displayed in the display_sc function that you can find in the child class. And, in the testbench itself, the display function that was defined in the base class has been called by the handle of child class sc.

So, to summarize, our child class inherited the base class members and allowed us to play around with it. I will be attaching the whole code later in this article.

Polymorphed

An obvious piece of gold that comes to mind when we hear the word shapeshifting is T-1000. It could change into whatever kind of shape it wanted to be in. We have such a concept in OOP as well. It is called polymorphism.

An obvious reason for this existence is that we might have a lot of functions in our system having almost similar functionality. Having them named differently might create a problem for us. This is why, it’s convenient to have a system where function calling is dependent on the handle of the class, instead of the class itself. This means, that every function or member of the class will get a different memory location to interact with regardless of the name it has. If we are using different handles to call those functions, we will get different outputs, doesn’t matter if the names of the functions that are being called are similar.

To give an example, here’s an example:

class first;
function display();
$display("This comes from first function");
endfunction
endclass
class second;
function display();
$display("This comes from second function");
endfunction
endclass
module tb;
first fc = new();
second sc = new();
initial begin
fc.display();
sc.display();
end
endmodule

As you can see, we have a similar function name display in both classes first and second. But as long as we have different handle name fc and sc announced for them in the testbench, it does not matter.

Class Assignment

Let’s recall something real quick now! In the constructor article of Day 2, I mentioned that nothing gets a memory location until the constructor function is called, or, in other words, the handle is instantiated as an object.

Now let’s look at this.

class first;
string name;

function void display();
$display("[First] My name is: %s",name);
endfunction
endclass

class second extends first;
string name;
function void display();
$display("[Second] My nationality is: %s",name);
endfunction
endclass
module tb;
first fc ;
second sc ;
initial begin
sc = new();

fc.name = "Eraz";
sc.name = "Bangladeshi";
fc.display();
sc.display();
end
endmodule

Even though second is derived class here, inheriting everything from class first, only instantiating second is not going to allocate memory for first as well. We have two options here. First, to instantiate the first class as well, or assign the handle of the second class into first which will allocate memory location to the first class as well and will let us access and use all the members or class properties. This assignment of one class to another can be done like this:

class first;
string name;
function void display();
$display("[First] My name is: %s",name);
endfunction
endclass
class second extends first;
string name;

function void display();
$display("[Second] My nationality is: %s",name);
endfunction
endclass
module tb;
first fc ;
second sc ;
initial begin
sc = new();
fc = sc;
fc.name = "Eraz";
sc.name = "Bangladeshi";
fc.display();
sc.display();
end
endmodule

Note here, that we assigned the handle of the second class to the first by just fc = sc syntax which is pretty straightforward and understandable.

But! Here is a catch! What if we instantiated the first class here only and assigned it to the second class? Would it create a memory location for the second class by doing this?

No! Because the child class(second class) will always consist of elements from the first class. This means the child class will always take up more space than the parent class. This is why, assigning parent class to child will not leave enough space for all the child class property to instantiate which might cause trouble and thus is not permissible. Doing it will throw an Error.

So the below example is not permissible.

class first;
string name;
function void display();
$display("[First] My name is: %s",name);
endfunction
endclass

class second extends first;
string name;
function void display();
$display("[Second] My nationality is: %s",name);
endfunction
endclass
module tb;
first fc ;
second sc ;
initial begin
fc = new();
sc = fc;
fc.name = "Eraz";
sc.name = "Bangladeshi";
fc.display();
sc.display();
end
endmodule

So that’s everything I know and experimented with, on Inheritance, Polymorphism, and Class Assignment. I hope this piece of knowledge will help the community to clear up confusion.

Disclaimer: The knowledge above is true and rigid by the confidence of all the experiments and resources I went through. But it’s not the holy grail of course! I cordially request by connections to add anything that I might have missed while defining class or maybe have mistaken or misinterpreted something. Your cooperation will help me learn and grow more!

If you are new to this article series and kind of lost, I started a 10-day discussion series about Object Oriented Programming using SystemVerilog. Follow the series to learn about my understanding of Object Oriented Programming and add your knowledge as well to make my understanding better in the comments.

--

--

Eraz Ahmed
0 Followers

Always in the quest of learning how the device I am writing this bio in works and can be improved.