Encapsulation issue

Hi All,

Please help on encapsulation.

  1. I have a simple following code and the behaviour of the code as follow below:
    1.Created local variable in base class and trying to set using method.
    2.Set local variable with 6 and printing same from base class handle and extended class handle. In case of base class handle it is showing proper value but when i call from extended class it is giving 0…

kindly help on this.

Here is the program:

class a;
local int a ;
function set(int x);
  a = x;
  $display("Setting Local Variable : a = %d, x = %d",a,x);
endfunction

function disp();
$display("Local Variable Value : %d",a);
endfunction

endclass

class b extends a;
function disp1();
  super.disp();
endfunction
endclass

program p1;
b b1;
a a1;
initial
begin
b1 = new();
a1 = new();
a1.disp();
b1.disp1();
a1.set(6);
a1.disp();
b1.disp1();
end
endprogram

a1.set(6) only sets the local class member variable in a1.

a1 and b1 have handles to two separate objects each with their own local class member ‘int a’ (BTW, it is incredibly confusing to use the same identifier ‘a’ for the class name and member name). When you construct class b and store its handle in b1, class b’s type is extended from class a, so it inherits all the members in class a.

so you need to call b1.set(6);

In reply to dave_59:

Thanks for your solution Dave.

But I am calling display method of base class from extended display method using super.disp; in this case it should print same value when we call directly using base class handle or indirectly from extended class using super.disp.

In reply to sivakrishna:

Inheritance is a very basic principle of object oriented programming and there are many online resources available to help you. Try https://verificationacademy.com/courses/introduction-to-the-uvm, specifically the section on object oriented programming.