Why is overridden class also being instantiate with set_type_override_by_type?

Hi, I am trying to understand the set_type/inst_override* UVM function better. With the following code, I am seeing some unexpected behavior.

I am trying to replace base_agent with child_agent in the “new_test”. The only difference between the two is the `uvm_info print in the “new” function. In the output I see that the new function of “base_agent” is being called for the “new_test” even though I am calling set_type_override_by_type(base_agent::get_type(), child_agent::get_type()). Any idea why? Is there a mistake in my use of the override function?

The eda playground with that code is here: UVM reuse demo - EDA Playground

import uvm_pkg::*;

class base_agent extends uvm_agent;
  `uvm_component_utils(base_agent);
  function new(string name, uvm_component parent);
    super.new(name, parent);
    `uvm_info("base_agent", "Base agent created", UVM_LOW);
  endfunction
endclass

class my_env extends uvm_env;
  `uvm_component_utils(my_env);
  base_agent agent1;
  base_agent agent2;
  function new(string name, uvm_component parent);
    super.new(name, parent);
    `uvm_info("base_agent", "Environment created", UVM_LOW);
  endfunction
  
  virtual function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    agent1 = base_agent::type_id::create("agent1", this);
    agent2 = base_agent::type_id::create("agent2", this);
  endfunction
endclass

class child_agent extends base_agent;
  `uvm_component_utils(child_agent);
  function new(string name, uvm_component parent);
    super.new(name, parent);
    `uvm_info("child_agent", "Child agent created", UVM_LOW);
  endfunction  
endclass

class new_test extends uvm_test;
  `uvm_component_utils(new_test);
  my_env myEnv;
  
  function new(string name, uvm_component parent);
    super.new(name, parent);
  endfunction
  
  virtual function void build_phase(uvm_phase phase);
    super.build_phase(phase);
    set_type_override_by_type(base_agent::get_type(), child_agent::get_type());
    myEnv = my_env::type_id::create("myEnv",this);
  endfunction
  
endclass


module top;
  initial begin
    run_test("new_test");
  end
endmodule

UVM_INFO @ 0: reporter [RNTST] Running test new_test…
UVM_INFO testbench.sv(21) @ 0: uvm_test_top.myEnv [base_agent] Environment created
UVM_INFO testbench.sv(11) @ 0: uvm_test_top.myEnv.agent1 [base_agent] Base agent created
UVM_INFO testbench.sv(53) @ 0: uvm_test_top.myEnv.agent1 [child_agent] Child agent created
UVM_INFO testbench.sv(11) @ 0: uvm_test_top.myEnv.agent2 [base_agent] Base agent created
UVM_INFO testbench.sv(53) @ 0: uvm_test_top.myEnv.agent2 [child_agent] Child agent created
UVM_INFO /apps/vcsmx/vcs/Q-2020.03-SP1-1//etc/uvm-ieee/src/base/uvm_report_server.svh(899) @ 0: reporter [UVM/REPORT/SERVER]
— UVM Report Summary —
** Report counts by severity
UVM_INFO : 7
UVM_WARNING : 0
UVM_ERROR : 0
UVM_FATAL : 0
** Report counts by id
[RNTST] 1
[UVM/RELNOTES] 1
[base_agent] 3
[child_agent] 2

In reply to Pooja Pathak:

The constructor of the extended class (child_agent) calls the constructor of the base class (base_agent). This is why you see the `uvm_info call from both classes. This is working as expected.

In reply to cgales:
Thanks!