This keyword

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

In reply to Shiv_coder:

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?

In reply to yourcheers:
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

In reply to Shiv_coder:

Hi,

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

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

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.

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

In reply to yourcheers:

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).