Data hiding and Encapsulation
Table of Contents
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 class members
External access to the class members can be avoided by declaring members as local.
Any violation could result in a compilation error.
Syntax:
local integer x;
Local Class members examples
Accessing local variable outside the class ( Not allowed )
In below example,
The local variable declared inside the class is trying to access from outside the class by using object handle.
As the variable is declared as local, which leads to a compilation error.
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'.
Accessing local variable within the class ( Allowed )
In the below example, The local variable declared inside the class is being accessed inside the class. as it is allowed, no compilation error is observed.
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
Protected class members
In some use cases, it is required to access the class members only by the derived class’s, this can be done by prefixing the class members with the protected keyword.
Any violation could result in a compilation error.
Syntax:
protected integer x;
Protected Class members examples
Accessing a protected variable outside the class ( Not allowed )
In the below example, The protected variable declared inside the class is trying to access from outside the class by using object handle.
As the variable is declared as protected, which leads to a compilation error.
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'.
Accessing a protected variable in the extended class ( allowed )
In the below example, The protected variable declared inside the class is being accessed inside the extended class. as it is allowed, no compilation error is observed.
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