Cover image: Virtual Vs Pure Virtual Methods | The Art of Verification

Virtual vs Pure Virtual Functions in SystemVerilog, with a Tested Example

In short: A virtual function in SystemVerilog has a body, and a child class can override it. A pure virtual function has no body, lives only in a virtual (abstract) class or an interface class, and every usable child must override it. With both, the call follows the object, not the handle.

This question comes up in almost every SystemVerilog interview, and it looks simple until a base handle calls the wrong version. So I put every rule in this post into one small file and ran it on VCS. The output blocks below are what it printed.

If you came here for C++, the idea is the same. The keywords and a couple of the rules are not, so the examples are SystemVerilog only.

A simple way to picture it

Think of a form template. A plain line on it is printed text: everyone gets the same words. A virtual line has default text that a team can cross out and rewrite, and whoever reads the form follows the rewritten version.

A pure virtual line is a blank marked “required”. The template itself can never be handed in, and no copy counts until that blank is filled. The template is the abstract class; a filled-in copy is a concrete child.

Virtual vs pure virtual function in SystemVerilog: a virtual call follows the object, a plain call follows the handle, and every child of an abstract class must implement the pure virtual method
Which method runs: a virtual call follows the object, a plain call follows the handle

Virtual function: the call follows the object

Mark a method virtual in the base class and a child can override it. When you call it through a base handle, the simulator looks at the object the handle points to and runs that class’s version.

A method without virtual works the other way. The handle’s type decides, at compile time, and the child’s version is never called through a base handle.

class tav_shape;
  virtual function real area();
    return 0.0;
  endfunction

  function string kind();          // plain method: no virtual
    return "shape";
  endfunction
endclass

class tav_circle extends tav_shape;
  real r;
  function new(real r);
    this.r = r;
  endfunction
  virtual function real area();
    return 3.14159 * r * r;
  endfunction
  function string kind();
    return "circle";
  endfunction
endclass

// in the test:
c = new(2.0);
s = c;           // base handle, circle object
s.area();        // tav_circle::area
s.kind();        // tav_shape::kind
UVM_INFO testbench.sv(214) @ 0: uvm_test_top [TAV_SHAPE] s.area() = 12.57  (virtual, base handle)
UVM_INFO testbench.sv(215) @ 0: uvm_test_top [TAV_SHAPE] s.kind() = shape  (plain, base handle)
UVM_INFO testbench.sv(216) @ 0: uvm_test_top [TAV_SHAPE] c.kind() = circle  (plain, circle handle)

Both methods exist in both classes. The only difference is the word virtual, and it decides which one runs.

Pure virtual function: the child has to write it

A pure virtual method is only a prototype: name, return type and arguments. It has no body and no endfunction line. It’s the base class saying “every real child of mine has this method, but I can’t tell you how it works”.

A class with a pure virtual method has to be declared virtual class, which makes it abstract. You can declare handles of that type, but you can’t call new() on it.

virtual class tav_pkt_base;
  bit [7:0] payload[$];

  pure virtual function bit [7:0] calc_crc();
  pure virtual task send();

  // a normal method in an abstract class, inherited as is
  function void fill(int n, bit [7:0] first);
    payload.delete();
    repeat (n) payload.push_back(first++);
  endfunction
endclass

class tav_pkt_xor extends tav_pkt_base;
  virtual function bit [7:0] calc_crc();
    bit [7:0] c = 0;
    foreach (payload[i]) c ^= payload[i];
    return c;
  endfunction

  virtual task send();
    #10 `uvm_info("TAV_PKT", $sformatf("xor pkt: %0d bytes, crc=%02h", payload.size(), calc_crc()), UVM_LOW)
  endtask
endclass

tav_pkt_sum is the same with += in place of ^=. The test fills each packet with 10, 11, 12, 13 (hex), puts the objects in one array of tav_pkt_base handles and loops over it. One loop, and each object runs its own calc_crc and send.

UVM_INFO testbench.sv(232) @ 0: uvm_test_top [TAV_PKT] pkts[0].calc_crc() = 00
UVM_INFO testbench.sv(232) @ 0: uvm_test_top [TAV_PKT] pkts[1].calc_crc() = 46
UVM_INFO testbench.sv(232) @ 0: uvm_test_top [TAV_PKT] pkts[2].calc_crc() = b9
UVM_INFO testbench.sv(82) @ 10000: reporter [TAV_PKT] xor pkt: 4 bytes, crc=00
UVM_INFO testbench.sv(94) @ 30000: reporter [TAV_PKT] sum pkt: 4 bytes, crc=46
UVM_INFO testbench.sv(94) @ 50000: reporter [TAV_PKT] sum pkt: 4 bytes, crc=b9

Look at the last line. The grandchild tav_pkt_sum_inv has no send() of its own, so it runs the one it inherited from tav_pkt_sum (line 94, “sum pkt”). But inside that send(), the call to calc_crc() still lands on the grandchild’s version, so the crc is b9, not 46.

This is the pattern UVM is built on, and the bench shows it too. run_phase is virtual in uvm_component, so when UVM runs the phase it calls tav_virt_test::run_phase through a base handle, without ever knowing the class name.

Rules that are easy to get wrong

Once virtual, always virtual. In the bench, tav_pkt_sum_inv extends tav_pkt_sum and overrides calc_crc without writing virtual. It’s still virtual, because the base declared it that way.

The override has to match. Same return type, same argument types and directions. Change real to int and the compiler stops you (see the mistakes below).

super reaches the parent’s version. tav_pkt_sum_inv::calc_crc returns ~super.calc_crc(), so it reuses the sum and inverts it. When the parent’s method is pure virtual, there’s no body for super to reach (mistake 5 below shows what VCS does with it).

Tasks work the same way. pure virtual task send(); is legal, and each child gives it a body with its own timing.

Interface classes are the other home. Every method in an interface class is a pure virtual prototype. A class that implements it has to write them all.

interface class tav_printable;
  pure virtual function string to_str();
endclass

class tav_reg implements tav_printable;
  ...
  virtual function string to_str();
    return $sformatf("%s=0x%08h", name, value);
  endfunction
endclass
UVM_INFO testbench.sv(245) @ 50000: uvm_test_top [TAV_REG] items[0] CTRL=0x00000001
UVM_INFO testbench.sv(245) @ 50000: uvm_test_top [TAV_REG] items[1] STATUS=0x80000000

A virtual call from inside the base constructor

This is the one that surprises people. tav_cfg_base::new() calls the virtual method describe(). The child, tav_cfg_child, overrides describe() to print its field lanes, which is set to 4 in its declaration.

The child’s field initializers run only after super.new() returns. So the question is which describe() runs inside the base constructor, and what it sees.

UVM_INFO testbench.sv(127) @ 50000: reporter [TAV_CFG] tav_cfg_base::new calls describe(): child, lanes=0
UVM_INFO testbench.sv(140) @ 50000: reporter [TAV_CFG] tav_cfg_child::new after super.new(): child, lanes=4

On VCS, the base constructor already ran the child’s describe(). But lanes was still 0, because lanes = 4 hadn’t run yet. One line later, back in the child’s constructor, the same call shows 4.

When I ran an earlier plain version of this file on Verilator 5.020, that first line said “base” instead. So two simulators disagree here, and neither answer is one you want. Keep virtual calls out of constructors; in UVM, do that work in build_phase.

Side by side

virtual pure virtual
Has a body Yes, a default No, prototype only
endfunction / endtask Yes No
Child overrides it Can Must, or the child is abstract too
Where it can be declared Any class virtual class or interface class
Class can be newed Yes No, the class is abstract
Call through a base handle Runs the object’s version Runs the object’s version
super.method() from the child Runs the parent body Nothing to run (see mistake 5)

Which one to use

Use a plain virtual method when the base has a sensible default and a child may want to change it. A UVM example is do_print or convert2string.

Use pure virtual when there’s no sensible default and forgetting the method should stop the build. A CRC, an encode step or a protocol-specific drive are good fits. The compiler then checks every child for you.

The bench: one file, six runs

Everything above comes from one file, tav_virtual_methods.sv (download it as a zip, 3 KB). It’s a small UVM test, and every message goes through uvm_info. Paste it into testbench.sv on EDA Playground, pick UVM 1.2, leave design.sv empty and set the top module to tav_top.

The clean run checks its own results and prints PASSED or FAILED. The other five runs each switch on one mistake with a define. VCS stopped runs 1 to 4 at compile time; run 5 compiled, which is a story of its own (mistake 5).

Run Compile option What it switches on
Clean (none) Parts 1 to 6
1 +define+TAV_ERR1 new() on the abstract tav_pkt_base
2 +define+TAV_ERR2 a plain child that skips calc_crc
3 +define+TAV_ERR3 pure virtual in a normal class
4 +define+TAV_ERR4 an area() override that returns int, not real
5 +define+TAV_ERR5 super.calc_crc() when the parent’s version is pure virtual

The full code: tav_virtual_methods.sv

// tav_virtual_methods.sv : virtual vs pure virtual methods, on one small class tree.
// The Art of Verification (theartofverification.com)
//
// Run on: Synopsys VCS with UVM 1.2 (EDA Playground).
// Paste into testbench.sv, leave design.sv empty. Top module: tav_top.
// All messages go through `uvm_info / `uvm_error.
//
// What it shows (clean run):
//   1. a virtual method follows the object, a plain method follows the handle
//   2. an abstract (virtual) class with pure virtual methods, two concrete children,
//      one loop over base handles
//   3. an override stays virtual in a grandchild even without the keyword
//   4. super.method() runs the parent version from inside an override
//   5. an interface class: pure virtual methods outside a virtual class
//   6. a virtual method called from inside the base constructor: which version
//      runs, and what it sees of the child's fields
// The test itself is the UVM case: run_phase is virtual in uvm_component, and UVM
// calls our override through a base handle.
//
// Compile-time mistakes, one at a time (VCS stops on 1 to 4; 5 compiles):
//   +define+TAV_ERR1   new() on an abstract class
//   +define+TAV_ERR2   a plain child that skips a pure virtual method
//   +define+TAV_ERR3   pure virtual method inside a normal (non-virtual) class
//   +define+TAV_ERR4   override with a different return type
//   +define+TAV_ERR5   super call to a pure virtual method

`timescale 1ns/1ps
`include "uvm_macros.svh"

package tav_virt_pkg;
  import uvm_pkg::*;

  // ----------------------------------------------------------- 1. virtual vs plain
  class tav_shape;
    virtual function real area();
      return 0.0;
    endfunction

    function string kind();          // plain method: no virtual
      return "shape";
    endfunction
  endclass

  class tav_circle extends tav_shape;
    real r;

    function new(real r);
      this.r = r;
    endfunction

    virtual function real area();
      return 3.14159 * r * r;
    endfunction

    function string kind();
      return "circle";
    endfunction
  endclass

  // ----------------------------------------------- 2. abstract class + pure virtual
  virtual class tav_pkt_base;
    bit [7:0] payload[$];

    pure virtual function bit [7:0] calc_crc();
    pure virtual task send();

    // a normal method in an abstract class, inherited as is
    function void fill(int n, bit [7:0] first);
      payload.delete();
      repeat (n) payload.push_back(first++);
    endfunction
  endclass

  class tav_pkt_xor extends tav_pkt_base;
    virtual function bit [7:0] calc_crc();
      bit [7:0] c = 0;
      foreach (payload[i]) c ^= payload[i];
      return c;
    endfunction

    virtual task send();
      #10 `uvm_info("TAV_PKT", $sformatf("xor pkt: %0d bytes, crc=%02h", payload.size(), calc_crc()), UVM_LOW)
    endtask
  endclass

  class tav_pkt_sum extends tav_pkt_base;
    virtual function bit [7:0] calc_crc();
      bit [7:0] c = 0;
      foreach (payload[i]) c += payload[i];
      return c;
    endfunction

    virtual task send();
      #20 `uvm_info("TAV_PKT", $sformatf("sum pkt: %0d bytes, crc=%02h", payload.size(), calc_crc()), UVM_LOW)
    endtask
  endclass

  // ----------------------------------------- 3. virtual is inherited by a grandchild
  class tav_pkt_sum_inv extends tav_pkt_sum;
    function bit [7:0] calc_crc();     // no 'virtual' keyword here, still virtual
      return ~super.calc_crc();        // 4. super runs tav_pkt_sum::calc_crc
    endfunction
  endclass

  // ---------------------------------------------------------- 5. interface class
  interface class tav_printable;
    pure virtual function string to_str();
  endclass

  class tav_reg implements tav_printable;
    string name;
    bit [31:0] value;

    function new(string name, bit [31:0] value);
      this.name  = name;
      this.value = value;
    endfunction

    virtual function string to_str();
      return $sformatf("%s=0x%08h", name, value);
    endfunction
  endclass

  // -------------------------------------- 6. virtual call from the base constructor
  class tav_cfg_base;
    function new();
      `uvm_info("TAV_CFG", {"tav_cfg_base::new calls describe(): ", describe()}, UVM_LOW)
    endfunction

    virtual function string describe();
      return "base";
    endfunction
  endclass

  class tav_cfg_child extends tav_cfg_base;
    int lanes = 4;                     // set only after super.new() returns

    function new();
      super.new();
      `uvm_info("TAV_CFG", {"tav_cfg_child::new after super.new(): ", describe()}, UVM_LOW)
    endfunction

    virtual function string describe();
      return $sformatf("child, lanes=%0d", lanes);
    endfunction
  endclass

  // -------------------------------------------------- compile-time mistakes
`ifdef TAV_ERR2
  class tav_pkt_lazy extends tav_pkt_base;   // skips calc_crc, and isn't virtual
    virtual task send();
    endtask
  endclass
`endif

`ifdef TAV_ERR3
  class tav_not_abstract;                      // missing 'virtual'
    pure virtual function void go();
  endclass
`endif

`ifdef TAV_ERR4
  class tav_circle_bad extends tav_shape;
    virtual function int area();               // base returns real
      return 1;
    endfunction
  endclass
`endif

`ifdef TAV_ERR5
  class tav_pkt_super extends tav_pkt_base;
    virtual function bit [7:0] calc_crc();
      return super.calc_crc();                 // parent has no body to run
    endfunction
    virtual task send();
    endtask
  endclass
`endif

  // ===================================================================== the test
  class tav_virt_test extends uvm_test;
    `uvm_component_utils(tav_virt_test)

    int unsigned n_fail;

    function new(string name, uvm_component parent);
      super.new(name, parent);
    endfunction

    function void tav_check(string what, bit ok);
      if (!ok) begin
        n_fail++;
        `uvm_error("TAV_CHECK", {"check failed: ", what})
      end
    endfunction

    // run_phase is virtual in uvm_component: UVM calls this override
    virtual task run_phase(uvm_phase phase);
      tav_shape       s;
      tav_circle      c;
      tav_pkt_base    pkts[3];
      tav_pkt_xor     px;
      tav_pkt_sum     ps;
      tav_pkt_sum_inv pi;
      tav_printable   items[2];
      tav_reg         r0, r1;
      tav_cfg_child   cfg;

      phase.raise_objection(this);

      // 1. virtual vs plain
      c = new(2.0);
      s = c;
      `uvm_info("TAV_SHAPE", $sformatf("s.area() = %0.2f  (virtual, base handle)", s.area()), UVM_LOW)
      `uvm_info("TAV_SHAPE", {"s.kind() = ", s.kind(), "  (plain, base handle)"}, UVM_LOW)
      `uvm_info("TAV_SHAPE", {"c.kind() = ", c.kind(), "  (plain, circle handle)"}, UVM_LOW)
      tav_check("area through base handle", s.area() > 12.56 && s.area() < 12.57);
      tav_check("kind through base handle", s.kind() == "shape");

`ifdef TAV_ERR1
      pkts[0] = new();                         // abstract class: not allowed
`endif

      // 2. one loop over base handles, three concrete objects
      px = new(); px.fill(4, 8'h10);
      ps = new(); ps.fill(4, 8'h10);
      pi = new(); pi.fill(4, 8'h10);
      pkts[0] = px;
      pkts[1] = ps;
      pkts[2] = pi;
      foreach (pkts[i])
        `uvm_info("TAV_PKT", $sformatf("pkts[%0d].calc_crc() = %02h", i, pkts[i].calc_crc()), UVM_LOW)
      tav_check("xor crc", pkts[0].calc_crc() == 8'h00);
      tav_check("sum crc", pkts[1].calc_crc() == 8'h46);
      tav_check("grandchild crc", pkts[2].calc_crc() == 8'hB9);

      foreach (pkts[i]) pkts[i].send();      // each object runs its own send()

      // 5. interface class
      r0 = new("CTRL",   32'h0000_0001);
      r1 = new("STATUS", 32'h8000_0000);
      items[0] = r0;
      items[1] = r1;
      foreach (items[i])
        `uvm_info("TAV_REG", $sformatf("items[%0d] %s", i, items[i].to_str()), UVM_LOW)
      tav_check("to_str", items[1].to_str() == "STATUS=0x80000000");

      // 6. virtual call from the base constructor
      cfg = new();
      tav_check("lanes after new", cfg.lanes == 4);

      phase.drop_objection(this);
    endtask

    virtual function void report_phase(uvm_phase phase);
      if (n_fail == 0) `uvm_info("TAV_SUMMARY", "*** TEST PASSED ***", UVM_NONE)
      else             `uvm_error("TAV_SUMMARY", $sformatf("*** TEST FAILED *** (%0d checks)", n_fail))
    endfunction
  endclass
endpackage

module tav_top;
  import uvm_pkg::*;
  import tav_virt_pkg::*;

  initial run_test("tav_virt_test");
endmodule

What VCS printed

VCS X-2025.06-SP1 with UVM 1.2, clean run:

UVM_INFO @ 0: reporter [RNTST] Running test tav_virt_test...
UVM_INFO testbench.sv(214) @ 0: uvm_test_top [TAV_SHAPE] s.area() = 12.57  (virtual, base handle)
UVM_INFO testbench.sv(215) @ 0: uvm_test_top [TAV_SHAPE] s.kind() = shape  (plain, base handle)
UVM_INFO testbench.sv(216) @ 0: uvm_test_top [TAV_SHAPE] c.kind() = circle  (plain, circle handle)
UVM_INFO testbench.sv(232) @ 0: uvm_test_top [TAV_PKT] pkts[0].calc_crc() = 00
UVM_INFO testbench.sv(232) @ 0: uvm_test_top [TAV_PKT] pkts[1].calc_crc() = 46
UVM_INFO testbench.sv(232) @ 0: uvm_test_top [TAV_PKT] pkts[2].calc_crc() = b9
UVM_INFO testbench.sv(82) @ 10000: reporter [TAV_PKT] xor pkt: 4 bytes, crc=00
UVM_INFO testbench.sv(94) @ 30000: reporter [TAV_PKT] sum pkt: 4 bytes, crc=46
UVM_INFO testbench.sv(94) @ 50000: reporter [TAV_PKT] sum pkt: 4 bytes, crc=b9
UVM_INFO testbench.sv(245) @ 50000: uvm_test_top [TAV_REG] items[0] CTRL=0x00000001
UVM_INFO testbench.sv(245) @ 50000: uvm_test_top [TAV_REG] items[1] STATUS=0x80000000
UVM_INFO testbench.sv(127) @ 50000: reporter [TAV_CFG] tav_cfg_base::new calls describe(): child, lanes=0
UVM_INFO testbench.sv(140) @ 50000: reporter [TAV_CFG] tav_cfg_child::new after super.new(): child, lanes=4
UVM_INFO .../uvm-1.2/src/base/uvm_objection.svh(1276) @ 50000: reporter [TEST_DONE] 'run' phase is ready to proceed to the 'extract' phase
UVM_INFO testbench.sv(256) @ 50000: uvm_test_top [TAV_SUMMARY] *** TEST PASSED ***

** Report counts by severity
UVM_INFO :   17
UVM_WARNING :    0
UVM_ERROR :    0
UVM_FATAL :    0

The messages from inside the packet and config classes say reporter, not uvm_test_top. Those classes aren’t UVM components, so uvm_info falls back to the global reporter.

Common mistakes (and the fix for each)

1. Calling new() on an abstract class. You can declare a tav_pkt_base handle, but the object has to be a concrete child. Fix: new() the child, then assign it to the base handle.

Error-[SV-ACCNBI] An abstract class cannot be instantiated
testbench.sv, 221
tav_virt_pkg, "pkts[0] = new();"
  Instantiation of the object 'pkts' can not be done because its type 
  'tav_pkt_base' is an abstract base class.
  Perhaps there is a derived class that should be used.

1 error

2. A child that skips a pure virtual method. If the child is a plain class, this is a compile error, not a quiet “it stays abstract”. Fix: write the method, or declare the child virtual class on purpose and finish the job one level down.

Error-[SV-VMNI] Virtual method not implemented
testbench.sv, 64
  Virtual method 'calc_crc' not implemented in class 'tav_pkt_lazy' (declared 
  in testbench.sv, at line 150).

1 error

Note where VCS points: line 64 is the pure virtual prototype in the base class, and the child that forgot it is only named in the message. In a big testbench, read the class name, not just the line.

3. pure virtual inside a normal class. Only a virtual class or an interface class can hold one. Fix: add virtual in front of class.

Error-[PVMNA] Pure virtual method not allowed
testbench.sv, 158
"tav_not_abstract::go"
  A pure virtual method cannot be defined in a non-abstract class 
  'tav_not_abstract'.

1 error

4. An override that doesn’t match. The return type and every argument type have to match the base. Fix: copy the prototype from the base class.

Error-[SV-IRT] Incompatible return types
testbench.sv, 164
tav_virt_pkg, "area"
  Definition of class function 'tav_shape::area' does not have the same return
  type as mentioned in the declaration at: "testbench.sv", 35.

1 error

5. Calling super on a pure virtual method. The parent has no body to run. Fix: put the shared code in a normal method in the base, and call that.

This one surprised me. With +define+TAV_ERR5, VCS compiled the file without an error or a warning and the run ended with TEST PASSED, the same as the clean run. slang also accepted it.

The bench never creates a tav_pkt_super object, so the bad call never runs. So in real code, don’t count on the compiler to catch this one. Keep super calls pointed at methods that have a body.

6. Forgetting virtual in the base. This one compiles and runs, which makes it the worst of the lot. The base handle quietly calls the base version, like s.kind() in part 1. Fix: decide in the base class which methods a child may change, and mark those virtual.

Where AI helps, and where it doesn’t

An AI assistant is quick at the boring parts: a class skeleton, the prototypes, a table like the one above. It’s also handy for explaining an error message in plain words.

Where it falls short is anything that depends on the tool. Ask it what part 6 prints and you’ll get a confident answer, but the only answer that counts is the one your simulator gives. Run the file.

Practice challenge

Add a third packet type, tav_pkt_parity, whose calc_crc returns the even parity of all payload bits in bit 0. Put it in the same pkts array and don’t touch the loop. If the loop needs changing, something in your class isn’t virtual.

Then make tav_shape abstract with a pure virtual area(), and add a tav_rect. What happens to the line return 0.0;?

FAQ

What is the difference between a virtual and a pure virtual function in SystemVerilog? A virtual function has a body, and a child may override it. A pure virtual function has no body, only lives in a virtual class or an interface class, and every concrete child must override it.

Can a virtual class have normal methods? Yes. tav_pkt_base has fill(), which every child inherits as it is. Only the pure virtual methods must be written by the child.

Does an override need the virtual keyword? No. Once a method is virtual in the base, it stays virtual in every child and grandchild. Writing it again is still good practice, because the reader sees it at once.

Can I call new() on a virtual class? No. You can declare handles of that type and point them at child objects.

Can a task be pure virtual? Yes: pure virtual task send();. Each child gives it a body, with any timing it needs.

Why are UVM methods like build_phase and run_phase virtual? So the UVM library can call your version through a base-class handle. That’s how phasing runs your code without knowing your class names.

References

  • IEEE 1800-2017, SystemVerilog standard: section 8.20 (virtual methods), 8.21 (abstract classes and pure virtual methods), 8.26 (interface classes).
  • Chris Spear and Greg Tumbush, SystemVerilog for Verification, 3rd edition, chapter 8 (advanced OOP).

Keep learning

Virtual methods sit on top of inheritance, so start with inheritance in SystemVerilog if the extends part is new to you. The base-handle trick in this post is what polymorphism in SystemVerilog is all about, and encapsulation covers who can see which fields. For the one kind of method that can never be virtual, see static methods.

To see virtual methods holding a whole testbench together, read the UVM testbench architecture guide. There’s more in the SystemVerilog category and the interview questions.