Encapsulation:

Many times we might use the Base Class or Base Class library provided by third party sources. By default, These Class Members are Public in nature. It means these Class Members can be accessed directly from outside of that Class. But sometimes Base Class providers may restrict how others can access the Class members as a safety/security measure, preventing corruption of internal states and logic. To support those restrictions.

Class Members can be declared with the following two Qualifiers:

  1. Local: It is available only to methods inside the class. Further, these local members are not visible within subclasses.
  2. Protected: It is a property or method has all of the characteristics of a local member, except that it can be inherited; it is visible to subclasses.

Let us understand both the above-mentioned Qualifiers with examples:

Example: Local Variable Qualifier with Error Result

class parent; 
  local int a; 
endclass 
 
program main; 
  initial 
    begin 
      parent p = new(); 
      p.a = 777; 
    end 
endprogram

Example: Local variable Qualifier access using the method

class parent; 
  local int a; 
  
  task set(int b); 
    a = b; 
    $display(a); 
  endtask 
endclass 
 
program main; 
  initial 
    begin 
      parent p = new(); 
      p.set(777); 
    end 
endprogram

Example: Local Variable Qualifier access in Subclass/Child Class

class parent; 
  local int a; 
endclass 
 
class child extends parent; 
  function new(); 
    a = 10; 
  endfunction 
endclass 

Example: Protected Variable Qualifier

class parent; 
  protected int a; 
endclass 
 
class child extends parent; 
  function new(); 
    a = 101; 
  endfunction 
endclass

About the author

The Art of Verification

Hi, I’m Hardik, and welcome to The Art of Verification.

I’m a Verification Engineer who loves to crack complex designs and here to help others commit to mastering Verification Skills through self-learning, System Verilog, UVM, and most important to develop that thought process that every verification engineer should have.

I’ve made it my mission to give back and serve others beyond myself.

I will NEVER settle for less than I can be, do, give, or create.

View all posts

1 Comment