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

Similar Posts

One Comment

Comments are closed.