The protected keyword allows access from the declaring
class and all classes that extend it. It's useful when child classes
need to access parent's internal data.
class Transaction;
protected bit [31:0] addr; // Child classes can access
protected bit [31:0] data;
local int internal_id; // Only this class can access
function new();
internal_id = $urandom();
endfunction
virtual function void display();
$display("Transaction: addr=0x%h, data=0x%h", addr, data);
endfunction
endclass
class WriteTransaction extends Transaction;
bit [3:0] strobe;
function void set_write(bit [31:0] a, bit [31:0] d, bit [3:0] s);
addr = a; // OK - protected, accessible in child
data = d; // OK - protected
strobe = s;
// internal_id = 0; // ERROR! local is not accessible
endfunction
virtual function void display();
$display("WRITE: addr=0x%h, data=0x%h, strobe=0x%h",
addr, data, strobe);
endfunction
endclass
When to Use protected vs local?
Use protected when child classes need to access
or modify the data. Use local when data should be
completely private to the class.