# Member variable to be only inherited by immediate child

**URL:** <https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Inheritance\
**Created:** [July 2, 2021, 12:11am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256 "2021-07-02T00:11:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhupeshpaliwal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/ee59a6/32.png) [@bhupeshpaliwal](https://verificationacademy.com/forums/u/bhupeshpaliwal)\
**Post date:** [July 2, 2021, 12:11am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/1 "2021-07-02T00:11:20Z")

</div>

Is this possible to make parent class member variable to be visible ONLY to immediate child and not further in inheritance hierarchy?

```verilog

virtual class parent;
  string s;
  // ..
endclass

class child extends parent;
  // s is accessible here
endclass

class grand-child extends child;
  // s is NOT accessible here
endclass

```

---

<div class="post-metadata">

**Author:** ![bdreku](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bdreku/32/4520_2.png) [@bdreku](https://verificationacademy.com/forums/u/bdreku)\
**Post date:** [July 2, 2021, 3:01am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/2 "2021-07-02T03:01:24Z")

</div>

_In reply to [bhupeshpaliwal](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256):_  
It’s not possible. By default all the variables are public until they are encapsulated using identifiers like “local” or “protected”. If declared string s as protected then all the subclasses will be able to access it.

Can you please brief your purpose here.

---

<div class="post-metadata">

**Author:** ![bhupeshpaliwal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/ee59a6/32.png) [@bhupeshpaliwal](https://verificationacademy.com/forums/u/bhupeshpaliwal)\
**Post date:** [July 2, 2021, 4:33am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/3 "2021-07-02T04:33:58Z")

</div>

_In reply to [bdreku](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/2):_  
I need to ensure ONLY IMMEDIATE child is able to access i.e. grand child should not able to access string s. With private/protected I am not able to achieve the same as shown below:

```verilog

// Case 1: private qualifier
virtual class parent;
  private string s;
  // ..
endclass
 
class child extends parent;
  // s is NOT accessible here
endclass
 
class grand-child extends child;
  // s is NOT accessible here
endclass

// Case 2: protected qualifier
virtual class parent;
  protected string s;
  // ..
endclass
 
class child extends parent;
  // s is accessible here
endclass
 
class grand-child extends child;
  // s is accessible here
endclass

```

Am I missing something?

---

<div class="post-metadata">

**Author:** ![Rsignori92](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@Rsignori92](https://verificationacademy.com/forums/u/Rsignori92)\
**Post date:** [July 2, 2021, 8:28am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/4 "2021-07-02T08:28:26Z")

</div>

_In reply to [bhupeshpaliwal](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/3):_

If you wanna the class child class to have restricted access to the variable s then you could declare it as private only in the child class removing the need to have it in the parent. Other way could be using a function to access that variable which encapsulate some sort of restricted access like looking for the parent class if not the main starting parent class then 100% is not the child. Hope i got the question. Regards

---

<div class="post-metadata">

**Author:** ![Rsignori92](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@Rsignori92](https://verificationacademy.com/forums/u/Rsignori92)\
**Post date:** [July 2, 2021, 8:28am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/5 "2021-07-02T08:28:57Z")

</div>

_In reply to [Rsignori92](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/4):_

I will work out on it to see if feasible. Regards

---

<div class="post-metadata">

**Author:** ![bhupeshpaliwal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/ee59a6/32.png) [@bhupeshpaliwal](https://verificationacademy.com/forums/u/bhupeshpaliwal)\
**Post date:** [July 2, 2021, 8:54am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/6 "2021-07-02T08:54:12Z")

</div>

_In reply to [Rsignori92](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/5):_  
Moving it to child class is not desired, Parent can have child-1 and child-2 … both able to access string s.

---

<div class="post-metadata">

**Author:** ![Rsignori92](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@Rsignori92](https://verificationacademy.com/forums/u/Rsignori92)\
**Post date:** [July 2, 2021, 9:12am UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/7 "2021-07-02T09:12:13Z")

</div>

_In reply to [bhupeshpaliwal](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/6):_

Get it i will post out something soon. Regards

---

<div class="post-metadata">

**Author:** ![KillSteal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@KillSteal](https://verificationacademy.com/forums/u/KillSteal)\
**Post date:** [July 4, 2021, 5:57pm UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/8 "2021-07-04T17:57:11Z")

</div>

I am just curious to understand more. I am getting a feeling that you are trying to implement something using the wrong ideas/tools.

What real purpose does “grand-child not accessing the variable” serve ? It’s hard for me to think of a use case that requires this.

---

<div class="post-metadata">

**Author:** ![Rsignori92](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@Rsignori92](https://verificationacademy.com/forums/u/Rsignori92)\
**Post date:** [July 5, 2021, 2:52pm UTC](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/9 "2021-07-05T14:52:48Z")

</div>

_In reply to [Rsignori92](https://verificationacademy.com/forums/t/member-variable-to-be-only-inherited-by-immediate-child/39256/8):_

I tried some experiments in order to break the inheritance unfortunately is not quite obvious IMO since the inheritance is still hard to break. anyway still not able to get the valid use cases honestly even if we extend from the child class you can still cast them in order to make them compatible to the parent class.

the structure is generally speaking this one:

1. parent

1. 2. child  
[list=1]  
3. g-child  
  
  
[/list]

going from bottom to top you are going to find the same parent even if you declare a variable to be local for instance assigned by a function which will check the parent from where the class is derived this approach is still broken since by casting you will still allow the access. Why not simply change the approach since your problem could be fixed in different way simply do not use the same class and derive them. Regards
