# Inheritance/ Same variable declared in derived class as parent class

**URL:** <https://verificationacademy.com/forums/t/inheritance-same-variable-declared-in-derived-class-as-parent-class/38022>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Inheritance\
**Created:** [September 2, 2020, 9:28pm UTC](https://verificationacademy.com/forums/t/inheritance-same-variable-declared-in-derived-class-as-parent-class/38022 "2020-09-02T21:28:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssakshi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@ssakshi](https://verificationacademy.com/forums/u/ssakshi)\
**Post date:** [September 2, 2020, 9:28pm UTC](https://verificationacademy.com/forums/t/inheritance-same-variable-declared-in-derived-class-as-parent-class/38022/1 "2020-09-02T21:28:32Z")

</div>

Hi,

I have a doubt in the following implementation:

```verilog
class parent_class;
  bit [31:0] addr;
  bit [31:0] data;
  function new();
    addr= 'hffff;
    data = 'h5555;
  endfunction
  
  task display();
    $display("Base Addr = %h and data %h",addr,data);
  endtask
endclass
 
class child_class extends parent_class;
  bit [31:0] data;
  bit [31:0] addr;
 
   function new();  
    addr='h8888;
    data = 'h1234;
  endfunction
  
  task display();
    super.display();
    $display("Ext Data = %h, addr = %h", addr,data);
  endtask
 
endclass
 
module inheritence;
  initial begin
    child_class c;
    parent_class b;
    b =new();
    c= new();
    $display("%p", c);
    c.display();
  end
endmodule

```

Output:  
‘{super:’{addr:'hffff, data:'h5555}, data:'h1234, addr:'h8888}  
Base Addr = 0000ffff and data 00005555  
Ext Data = 00008888, addr = 00001234

Why does the addr and data values from the base class used in super.display() which is called from extended class?

Whereas if I overwrite the value of addr and data in the extended class, without explicitly declaring addr and data, then extended class values are printed using super.display() function. The code is as follows:

```verilog
class parent_class;
  bit [31:0] addr;
  bit [31:0] data;
  function new();
    addr= 'hffff;
    data = 'h5555;
  endfunction
  
  task display();
    $display("Base Addr = %h and data %h",addr,data);
  endtask
endclass
 
class child_class extends parent_class;
 
   function new();  
    addr='h8888;
    data = 'h1234;
  endfunction
  
  task display();
    super.display();
    $display("Ext Data = %h, addr = %h", addr,data);
  endtask
 
endclass
 
module inheritence;
  initial begin
    child_class c;
    parent_class b;
    b =new();
    c= new();
    $display("%p", c);
    c.display();
  end
endmodule

```

Output:  
‘{super:’{addr:'h8888, data:'h1234}}  
Base Addr = 00008888 and data 00001234  
Ext Data = 00008888, addr = 00001234

Thanks,  
Sakshi

---

<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:** [September 3, 2020, 6:47am UTC](https://verificationacademy.com/forums/t/inheritance-same-variable-declared-in-derived-class-as-parent-class/38022/2 "2020-09-03T06:47:11Z")

</div>

_In reply to [ssakshi](https://verificationacademy.com/forums/t/inheritance-same-variable-declared-in-derived-class-as-parent-class/38022):_

See [How can I use parent class's member variable in systemverilog? | Verification Academy](https://verificationacademy.com/forums/systemverilog/how-can-i-use-parent-classs-member-variable-systemverilog#reply-70711)

Use of “super” with %p seems to be specific to one tool.
