Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

local vs protected: Accessing a protected Member From a Child Class

SystemVerilog Advanced Features

Runnable example from the SystemVerilog local vs protected tutorial (Accessing a protected Member From a Child Class)

Open in IDE
testbench.sv
// Define a base class and let the variable be "protected" to this class
class ABC;
  protected byte protected_var;
endclass

// Define another class that extends ABC and have a function that tries
// to access the protected variable in ABC
class DEF extends ABC;  
  function show();
    $display ("protected_var = 0x%0h", protected_var);
  endfunction
endclass

module tb;
  initial begin
  
    // Create a new object of the child class, and call the show method
    // This works fine: unlike "local", "protected" members CAN be accessed
    // by methods of a child class
    DEF def = new();
    def.show();

  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.