Systemverilog Data Hiding and Encapsulation

 Data hiding and Encapsulation 


The technique of hiding the data within the class and making it available only through the methods, is known as encapsulation

because it seals the data (and internal methods) safely inside the "capsule" of the class, where it can be accessed only by trusted users  (i.e., by the methods of the class).

 Access Control : 

By default all the members and methods of a class are accessible from anywhere using the object handle, sometimes this could corrupt the class members values, which should not be touched at all.

Access control rules that restrict the members of a class from being used outside the class, this is achieved by prefixing the class members with the keywords,

  • local
  • protected
local:-
External access to the class members can be avoided  by declaring members as local.
Any violation could result in compilation error.

Syntax:
             local integer x;

 Example-1:  Accessing local variable outside the class ( Not allowed )

class parent_class;
  local bit [31:0] tmp_addr;
  
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction

  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass


//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
       
    p_c.tmp_addr = 20;  //Accessing local variable outside the class
    p_c.display();
  end
endmodule

 Simulator Output  

Error- Illegal class variable access
testbench.sv,
Local member 'tmp_addr' of class 'parent_class' is not visible to scope
'encapsulation'.
Execute the above code on 

 Example-2:   Accessing local variable within the class ( Allowed )

class parent_class;
  local bit [31:0] tmp_addr;
  
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction

  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass

//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
    p_c.display();
  end
endmodule

 Simulator Output  

Addr = 15
Execute the above code on 

protected :-
In some use cases it is requires to access the class members only by the derived class's, this can be done by prefixing the class members with protected keyword.
Any violation could result in compilation error.

Syntax:
            protected integer x;

 Example-1:  Accessing protected variable outside the class ( Not allowed )

class parent_class;
  protected bit [31:0] tmp_addr;
  
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction

  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass

class child_class extends parent_class;
  function new(bit [31:0] r_addr);
    super.new(r_addr);
  endfunction
  
  function void incr_addr();
    tmp_addr++;
  endfunction  
endclass

//   module
module encapsulation;
  initial begin
    parent_class p_c = new(5);
    child_class  c_c = new(10);
        
    // variable declared as protected cannot be accessed outside the class 
    p_c.tmp_addr = 10;
    p_c.display();
   
    c_c.incr_addr();  //Accessing protected variable in extended class
    c_c.display();
  end
endmodule

 Simulator Output  

Error- Illegal class variable access
testbench.sv,
Protected member 'tmp_addr' of class 'parent_class' is not visible to scope
'encapsulation'.
Execute the above code on 

 Example-2:  Accessing protected variable in the extended class ( allowed )

class parent_class;
  protected bit [31:0] tmp_addr;
  
  function new(bit [31:0] r_addr);
    tmp_addr = r_addr + 10;
  endfunction

  function display();
    $display("tmp_addr = %0d",tmp_addr);
  endfunction
endclass

class child_class extends parent_class;
  function new(bit [31:0] r_addr);
    super.new(r_addr);
  endfunction
  
  function void incr_addr();
    tmp_addr++;
  endfunction  
endclass

//   module
module encapsulation;
  initial begin
    child_class  c_c = new(10);
    
    c_c.incr_addr();  //Accessing protected variable in extended class
    c_c.display();
  end
endmodule

 Simulator Output  

Addr = 21
Execute the above code on