SystemVerilog Abstract Classes and Virtual Methods

 Abstract Classes and Virtual Methods 


An abstract class is a class that is declared abstract by specifying the class to be virtual.
Abstract classes cannot be instantiated, but they can be subclassed.
    virtual class packet;

Abstract class is used as prototype for the subclasses.

Abstract classes can also have virtual methods. Virtual methods are a basic polymorphic construct. A virtual method overrides a method in all the base classes, whereas a normal method only overrides a method in that class and its descendants.
--TODO---

 Example-1: 

In the example below,
creating the object of virtual class gives an compilation error.

//abstract class
virtual class packet;
  bit [31:0] addr;

  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

module virtual_class;
  initial begin
    packet p;
    p = new();
    p.display();
  end
endmodule

 Simulator Output  
virtual_class, "p = new();"
Instantiation of the object 'p' can not be done because its type 'packet' is
an abstract base class.
Perhaps there is a derived class that should be used.
Execute the above code on