How to override multiple instances of a base class with derived classes in one run?

I wanted to override one instance of NOC_base to Agent and remaining two instances to fabric. How can I achieve this?

You can override all instances by type, and then override the specific instance by inst. Here is an example.

`include "uvm_macros.svh"
import uvm_pkg::*;
class base extends uvm_component;
  `uvm_component_utils(base);
  function new(string name,uvm_component parent);
    super.new(name,parent);
  endfunction
  function void build_phase(uvm_phase phase);
    uvm_report_info(get_type_name(),"build");
  endfunction
endclass
class agent extends base;
  `uvm_component_utils(agent);
  function new(string name,uvm_component parent);
    super.new(name,parent);
  endfunction
endclass
class fabric extends base;
  `uvm_component_utils(fabric);
  function new(string name,uvm_component parent);
    super.new(name,parent);
  endfunction
endclass
class env extends uvm_env;
  `uvm_component_utils(env);
  function new(string name,uvm_component parent);
    super.new(name,parent);
  endfunction
  base base_[4];
  function void build_phase(uvm_phase phase);
    foreach(base_[i])
      base_[i] = base::type_id::create($sformatf("base_%0d_",i),this);
  endfunction
endclass
class test extends uvm_test;
  `uvm_component_utils(test);
  function new(string name,uvm_component parent);
    super.new(name,parent);
  endfunction
  env env_;
 function void build_phase(uvm_phase phase);
   set_type_override_by_type(base::get_type(), agent::get_type());          
   set_inst_override_by_type("env_.base_3_", base::get_type(), fabric::get_type());          
   env_ = env::type_id::create("env_",this);
  endfunction
endclass
module top;
  initial begin
    run_test("test");
  end
endmodule

Thankyou so much dave_59. This worked

Can’t we use just set_inst_override_by_type for all instances?

Type override is much more efficient–no dealing with regular expression matching in strings. Plus you have to worry about the ordering of inst overrides, and there is no way to remove/replace an inst override.