# Inheritance overridden member - memory allocation issue

**URL:** <https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Inheritance\
**Created:** [October 14, 2016, 7:33pm UTC](https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424 "2016-10-14T19:33:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shahkavish77](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@shahkavish77](https://verificationacademy.com/forums/u/shahkavish77)\
**Post date:** [October 14, 2016, 7:33pm UTC](https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424/1 "2016-10-14T19:33:10Z")

</div>

Hi,

I want to understand memory allocation while overriding member in concept of inheritance.  
My code is -

```verilog
class a;
  int i = 1;
endclass : a

class b;
  int i = 2;
endclass : b

module top;
  a a1;
  b b1;
  int p1,p2;

  initial begin
    b1 = new;
    a1 = b1;
    p1 = a1.i;
    p2 = b1.i;
  end
  
endmodule

```

Now I understand here that as there is only one memory created as only one new is called. And b1 is just assigned to a1.  
**Result** - p1 will be 1 and p2 will be 2.

**Question** - If there was only one memory allocated here, how come i got a1.i as well as b1.i later ?  
As per my understanding, If there was only one memory created in this case, ideally either value of i should get overloaded & i should be able to access any one value of i only rather than both.  
Please explain me memory allocation while overriding members here…

---

<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:** [October 14, 2016, 8:14pm UTC](https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424/2 "2016-10-14T20:14:22Z")

</div>

_In reply to [shahkavish77](https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424):_  
You are correct that only one object gets created, but the type of that object class **b** , which is extended from class **a**. When you extend a class type from another class type, that new class type contains everything in both classes. So when you construct an object of a class **b** type, both **int i** member variables get allocated. Which i variable you access depends on the class variable used to reference the object.

Please see my [SystemVerilog OOP course](https://verificationacademy.com/topics/systemverilog/systemverilog-oop-for-uvm-verification/), especially the second session on inheritance.

---

<div class="post-metadata">

**Author:** ![shahkavish77](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/d6d6ee/32.png) [@shahkavish77](https://verificationacademy.com/forums/u/shahkavish77)\
**Post date:** [October 14, 2016, 9:21pm UTC](https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424/3 "2016-10-14T21:21:07Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/inheritance-overridden-member-memory-allocation-issue/32424/2):_

Thanks Dave. Got clear idea and superb videos.
