Factory overriding

Hey All.

In the factory overriding is it follow the same concept of inheritance. I am getting same kind of error , I want to confirm that

For example

class parent ;
 int a = 7;
  intt b = 5 ; 
endclass

class child extents parent ;
 int a = 4 ;
 int d = 6 ;
endclass

module top();
 
  parent p ;
  child c  ;
  
initial begin
  c = new()
  p = c ;

 $display("a %d", p.a); // a = 4
 $display("d %d", p.d); // ERROR d is not present
end

endmodule

I am trying to override the env . But it is not recognizing the variable those declared in extended env.

This is the correct behavior. From the class variable p when using the factory, you do not know if p has a handle to a parent or child object, so referencing p.d is illegal. You can only reference class members known to the type of the class variable. And p.a should be 7, not 4.

The proper way to handle extended classes is by using SystemVerilog virtual methods. A search of that term should provide links to various tutorials. Instead of access class members directly, you provide virtual methods that represent the behavior you want for that class. In your example, you want to display the members of the object, so you might create a display method (In the UVM you would create a do_print or convert2string method). So the way to write your example is

class base_c;
   int a = 7;
   int b = 5 ; 
   virtual function void display;
      $display("base members");
      $display(" a %0d",a);
      $display(" b %0d",b);
   endfunction
endclass

class derived_c extends base_c;
   int 	   a = 4 ;
   int 	   d = 6 ;
   virtual function void display;
      super.display();
      $display("derived members");
      $display(" a %0d",a);
      $display(" d %0d",d);
   endfunction
endclass

module top();
   base_c base_h ;
   derived_c  derived_h  ;
   
   initial begin
      base_h = new();
      base_h.display();
      derived_h = new() ;
      base_h = derived_h ;
      base_h.display();
   end
endmodule

which should display

# base members
#  a 7
#  b 5
# base members
#  a 7
#  b 5
# derived members
#  a 4
#  d 6
#  quit -f

By the way, I do not like using the terms parent and child as class names when talking about inheritance; it makes is seem like when you construct a child, it is a separate object from the parent. I’d rather use base_c and derived_c to show that derived_c is a modified version of base_c that has added or overridden functionality. And I use _c to distinguish the class type from _h, which is a class variable that has handle to a class object.