# Class member variable re-declaration

**URL:** <https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Inheritance, classes\
**Created:** [July 11, 2017, 4:13pm UTC](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288 "2017-07-11T16:13:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![evilpascal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@evilpascal](https://verificationacademy.com/forums/u/evilpascal)\
**Post date:** [July 11, 2017, 4:13pm UTC](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/1 "2017-07-11T16:13:49Z")

</div>

Why isn’t this re-declaration of arr array flagged as an error?

Doesn’t my\_class2 already contain member variable arr?

```verilog
class my_class;  
  bit [7:0] arr [10];  
endclass

class my_class2 extends my_class;  
  bit [7:0] arr [15];  
endclass

module tb;
  my_class obj;
  my_class2 obj2;
  
  initial begin
    obj = new();
    obj2 = new();
    $display("arr size in obj is %0d", $size(obj.arr));
    $display("arr size in obj2 is %0d", $size(obj2.arr));
  end
endmodule : tb

```

Terminal output:

> arr size in obj is 10  
> arr size in obj2 is 15

---

<div class="post-metadata">

**Author:** ![frodus](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/f/e95f7d/32.png) [@frodus](https://verificationacademy.com/forums/u/frodus)\
**Post date:** [July 11, 2017, 4:52pm UTC](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/2 "2017-07-11T16:52:02Z")

</div>

Why should it be it simply resides inside of a different namespace and can still be accessed.  
Like shown in the following example:

```verilog

class my_class;  
  bit [7:0] arr [10];  
endclass
 
class my_class2 extends my_class;  
  bit [7:0] arr [15];  

  function ret_super_arr ();
    $display("arr size in super.arr is %0d", $size(super.arr));
  endfunction
endclass
 
module tb;
  my_class obj;
  my_class2 obj2;
 
  initial begin
    obj = new();
    obj2 = new();
    $display("arr size in obj is %0d", $size(obj.arr));
    $display("arr size in obj2 is %0d", $size(obj2.arr));
    obj2.ret_super_arr();
  end
endmodule : tb

```

---

<div class="post-metadata">

**Author:** ![sbellock](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sbellock](https://verificationacademy.com/forums/u/sbellock)\
**Post date:** [July 11, 2017, 5:28pm UTC](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/3 "2017-07-11T17:28:17Z")

</div>

_In reply to [evilpascal](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/2):_

SystemVerilog supports overriding class members, so that is perfectly legal. You can do the same thing with tasks and functions.

---

<div class="post-metadata">

**Author:** ![evilpascal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@evilpascal](https://verificationacademy.com/forums/u/evilpascal)\
**Post date:** [July 12, 2017, 8:20am UTC](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/4 "2017-07-12T08:20:26Z")

</div>

I always did the overriding of tasks and functions, but never class members.

With namespaces now in mind, it makes perfect sense.  
Thank you for the answers.

---

<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:** [July 12, 2017, 2:45pm UTC](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/5 "2017-07-12T14:45:02Z")

</div>

_In reply to [evilpascal](https://verificationacademy.com/forums/t/class-member-variable-re-declaration/33288/4):_

WARNING: although it is possible to override class members, it creates many problems and is not recommended.Most of the time it is done by mistake when copy/pasting a base class into a derived class and inadvertently copying the member variables.

One such example problem is with constraints. When you override a class member, the constraints specified in the base class do not get applied to the overridden member.

```verilog
class A;
  rand int my_int;
  constraint c {my_int > 5;}
endclass 
class B extends A;
rand int my_int;
  constraint c {my_int < 10;}
endclass

```

Some people incorrectly assume that my\_int is constrained to be 6,7,8, or 9.
