# This keyword

**URL:** <https://verificationacademy.com/forums/t/this-keyword/39437>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [August 15, 2021, 7:16pm UTC](https://verificationacademy.com/forums/t/this-keyword/39437 "2021-08-15T19:16:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shiv\_coder](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@Shiv\_coder](https://verificationacademy.com/forums/u/Shiv_coder)\
**Post date:** [August 15, 2021, 7:16pm UTC](https://verificationacademy.com/forums/t/this-keyword/39437/1 "2021-08-15T19:16:38Z")

</div>

My question is what if we use “this.property\_name” inside an extended class and the property “property\_name” is defined inside the parent class,so because the child class can access the properties of parent class so can the “this” keyword be used or will this return an error. I know this is a stupid question to ask but I still wanted to confirm it.(Like I know we can use super keyword to access the parent properties but I had this doubt about “this” keyword).

class parent;  
int property\_name;  
endclass

class child;  
this.property\_name=xyz;  
endclass

---

<div class="post-metadata">

**Author:** ![yourcheers](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/y/5f9b8f/32.png) [@yourcheers](https://verificationacademy.com/forums/u/yourcheers)\
**Post date:** [August 15, 2021, 7:35pm UTC](https://verificationacademy.com/forums/t/this-keyword/39437/2 "2021-08-15T19:35:58Z")

</div>

_In reply to [Shiv\_coder](https://verificationacademy.com/forums/t/this-keyword/39437):_

```verilog
class abc;
  int a = 100;
  
  function disp();
    $display(a);
  endfunction 
endclass

class ab extends abc;
  
  function disp1();
    this.disp();
  endfunction
endclass

module aa;
  ab a1 = new();
 
  initial begin
    a1.disp();
  end 
  
endmodule

```

Is this what you are looking for?

---

<div class="post-metadata">

**Author:** ![Shiv\_coder](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/85f322/32.png) [@Shiv\_coder](https://verificationacademy.com/forums/u/Shiv_coder)\
**Post date:** [August 16, 2021, 3:54am UTC](https://verificationacademy.com/forums/t/this-keyword/39437/3 "2021-08-16T03:54:00Z")

</div>

_In reply to [yourcheers](https://verificationacademy.com/forums/t/this-keyword/39437/2):_  
Hi @yourcheers thank you for the reply  
Yes I had the same doubt but instead of using the method disp() using this keyword can we use  
this keyword with the property ‘a’ without defining the property ‘a’ inside the class ‘ab’.like I have done below or will the below code give an error because this keyword is used for current instance of class but ‘a’ is not defined in the current class it is defined in the class abc so this keyword should not work for property ‘a’.

class abc;  
int a = 100;

function disp();  
$display(a);  
endfunction  
endclass

class ab extends abc;

int xyz;  
this.a=xyz;

function disp1();  
this.disp();  
endfunction  
endclass

module aa;  
ab a1 = new();

initial begin  
a1.disp();  
end

endmodule

---

<div class="post-metadata">

**Author:** ![vardhana](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vardhana](https://verificationacademy.com/forums/u/vardhana)\
**Post date:** [August 16, 2021, 4:14am UTC](https://verificationacademy.com/forums/t/this-keyword/39437/4 "2021-08-16T04:14:03Z")

</div>

_In reply to [Shiv\_coder](https://verificationacademy.com/forums/t/this-keyword/39437/3):_

Hi,

I had modified your code a bit, have a look below.

```verilog
class base;
int a;

function disp();
$display(a);
endfunction
endclass

class ext extends base;

int xyz;

function disp1();
  this.a=xyz;
  this.disp();
endfunction

endclass

module tb;
ext a1 = new();

initial begin
  a1.xyz=10;
a1.disp1();
end

endmodule

```

in above, value 10 will be displayed.

if we modify the extended class to include another function of name disp, then the function disp in base class is hidden

```verilog
class base;
int a;

function disp();
$display(a);
endfunction
endclass

class ext extends base;

int xyz;

function disp();
$display("Extended Display");
endfunction

function disp1();
  this.a=xyz;
  this.disp();
endfunction

endclass

module tb;
ext a1 = new();

initial begin
  a1.xyz=10;
a1.disp1();
end

endmodule

```

for above output will display text Extended Display.

to access disp function from base class, then we use super keyword as below.

```verilog
class base;
int a;

function disp();
$display(a);
endfunction
endclass

class ext extends base;

int xyz;

function disp();
$display("Extended Display");
endfunction

function disp1();
  this.a=xyz;
  super.disp();
endfunction

endclass

module tb;
ext a1 = new();

initial begin
  a1.xyz=10;
a1.disp1();
end

endmodule

```

the above code will display value 10

---

<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 16, 2021, 6:38am UTC](https://verificationacademy.com/forums/t/this-keyword/39437/5 "2021-08-16T06:38:55Z")

</div>

_In reply to [yourcheers](https://verificationacademy.com/forums/t/this-keyword/39437/4):_

I recommend avoiding using terms _parent_ & _child_ when referring to class inheritance—it is a type hierarchy. Just like humans are mammals; they inherit the property of hair and mammary glands. A human IS-A mammal, they are not two distinct instances.

The **this** keyword is an implicit handle to a class instance. You have access to whatever class methods and properties are part of the current class type. When you reference an identifier without the this keyword, the search rules say to look for that identifier using the following search rules from the point where the reference occurs:

1. Look in the local scope of the class method
2. Look in the scope of the current class
3. Look in the scope of the base class (the class that the current class got extended from)
4. Keep looking recursively to the base of the previous class until there are no more base classes.
5. Start looking outside the current class into the package or module where the class was defined.

The **this** keyword is a way of keeping the search within the class type(rules 2-4), skipping variables declared within the method as well as anything outside the class type hierarchy. The **super** keyword is a way of keeping the search in the base classes(riles 3-4).
