How to Call Only Parent Method with child Handle

In reply to Harjot:

You are confusing the terms ‘base’/‘extended’ with ‘parent’/‘child’. There is a distinct difference between these terms in SystemVerilog. An ‘extended’ class inherits all of the properties of a ‘base’ class. A ‘parent’ class contains an instance of a completely different ‘child’ class.

Your question should be phrased as “How to call a base-class method with an extended-class handle”.

To accomplish this, you assign the extended class handle to a base class handle, and call the required function:


class base_class;
  function void display();
    $display("Inside base");
  endfunction
endclass

class ext_class_1 extends base_class;
  function void display();
    $display("Inside extended class 1");
  endfunction
endclass

module testbench();
  ext_class_1 my_ext_class;
  base_class my_base_class;
  
  initial begin
    my_ext_class = new();
    my_ext_class.display();
    my_base_class = my_ext_class; // Assign the extended class handle to the base class handle
    my_base_class.display();
  end
endmodule