# How can I use parent class's member variable in systemverilog?

**URL:** <https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Inheritance\
**Created:** [August 8, 2018, 12:44am UTC](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824 "2018-08-08T00:44:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![UVM\_LOVE](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/uvm_love/32/1684_2.png) [@UVM\_LOVE](https://verificationacademy.com/forums/u/UVM_LOVE)\
**Post date:** [August 8, 2018, 12:44am UTC](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824/1 "2018-08-08T00:44:26Z")

</div>

Dear All,

I’m trying to understand an overriding class member in systemverilog with the below example code, As I understand, If I want to override then do use “extend” such as class child\_class extends parent\_class. if then, I can override the parent’s variables.

```verilog
 class parent_class;
  bit [31:0] addr;
  
  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

 class child_class extends parent_class;
  bit [31:0] data;
  
  function display();
    $display("Data = %0d",data);
  endfunction
endclass

module inheritence;
  initial begin
    parent_class d=new();
    child_class c=new();
    c.addr = 10;
    c.data = 20;
    d.addr = 30;
    c.addr = 10;
    
    c.display();
    d.display();
  end
endmodule

```

and I’ve got the below result,  
Data = 20  
Addr = 30

but I thought that it supposed to be  
Data = 20  
Addr = 10 not 30.

How can I use parent’s member ?  
Is there any way to implement overriding without super?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 8, 2018, 1:19am UTC](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824/2 "2018-08-08T01:19:22Z")

</div>

_In reply to [sylee](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824):_

I recommend against using the terms _parent_ and _child_ when referring to OOP inheritance. Parents create(construct) children and they are distinct objects from their parents. When you inherit property, that property becomes yours and all your property is part of one object.

You constructed two distinct objects that both share a common base class type. So   
c.addr and   
d.addr are two separate variables. However, your extended class “child\_class” has direct access to   
addr defined in its base class. So you could have written the extended class function as

```verilog
 function display();
    $display("Addr = %0d",addr);
    $display("Data = %0d",data);
  endfunction

```

But it’s better to call super.display(). That way if you change the base class, you only need to modify the base class and not all of its extensions.

I highly recommend watching my course on [SystemVerilog OOP](https://verificationacademy.com/topics/systemverilog/systemverilog-oop-for-uvm-verification/).

---

<div class="post-metadata">

**Author:** ![UVM\_LOVE](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/uvm_love/32/1684_2.png) [@UVM\_LOVE](https://verificationacademy.com/forums/u/UVM_LOVE)\
**Post date:** [August 8, 2018, 1:26am UTC](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824/3 "2018-08-08T01:26:52Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824/2):_

> _In reply to [sylee](https://verificationacademy.com/forums/t/how-can-i-use-parent-classs-member-variable-in-systemverilog/34824/2):_  
> I recommend against using the terms _parent_ and _child_ when referring to OOP inheritance. Parents create(construct) children and they are distinct objects from their parents. When you inherit property, that property becomes yours and all your property is part of one object.  
> You constructed two distinct objects that both share a common base class type. So   
> c.addr and   
> d.addr are two separate variables. However, your extended class “child\_class” has direct access to   
> addr defined in its base class. So you could have written the extended class function as

```verilog
 function display();
$display("Addr = %0d",addr);
$display("Data = %0d",data);
endfunction

```

But it’s better to call super.display(). That way if you change the base class, you only need to modify the base class and not all of its extensions.  
I highly recommend watching my course on [SystemVerilog OOP](https://verificationacademy.com/topics/systemverilog/systemverilog-oop-for-uvm-verification/).

Thanks, that’s what I want to know that between super and another way.
