Tutorials

Learn More

An abstract class is a special type of base class that is not intended to be instantiated and a set of derived classes can be created. 

  1. An abstract class is an incomplete class that may contain method implementation or may contain only the prototype of methods without actual implementation (known as pure virtual methods). It can not be instantiated and it can only be derived.
  2. The virtual keyword is used in front of the class to differentiate it from the normal class.
  3. An abstract class is also known as a virtual class.
  4. Method type, number of arguments, and return type (if required) should be the same for the virtual methods in their derived classes.
  5. It is not mandatory to add methods in the abstract class. 

Syntax:

virtual class <class_name>
  ...
endclass

Advantages of Abstract class

  1. To keep the program organized and understandable, it forms a group of classes.
  2. Common methods can be placed in the abstract class and these methods can be inherited by derived classes.

Without virtual keyword in the class

Example:

class parent_trans;
  bit [31:0] data;
  int id;
  
  function void display();
    $display("Base: Value of data = %0h and id = %0h", data, id);
  endfunction
endclass

module class_example;
  initial begin
    parent_trans p_tr;
    p_tr = new();
    
    p_tr.data = 5;
    p_tr.id = 1;
    p_tr.display();
  end
endmodule

Output:

Base: Value of data = 5 and id = 1

An Abstract class instantiation

As discussed earlier abstract class is not expected to be instantiated. In the below example, a compilation error is expected for the abstract class

Example:

virtual class parent_trans;
  bit [31:0] data;
  int id;
  
   function void display();
     $display("Base: Value of data = %0h and id = %0h", data, id);
  endfunction
endclass

module class_example;
  initial begin
    parent_trans p_tr;
    p_tr = new();
    
    p_tr.data = 5;
    p_tr.id = 1;
    p_tr.display();
  end
endmodule

Output:

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

Examples for abstract class

An abstract class with a derived class

A derived class can be extended from an abstract class is the same as how it is extended normally using the extends keyword. The child class is instantiated as shown in the below example.

virtual class parent_trans;
  bit [31:0] data;
  int id;
  
  function void display();
     $display("Base: Value of data = %0h and id = %0h", data, id);
  endfunction
endclass

class child_trans extends parent_trans;
  function void display();
    $display("Child: Value of data = %0h and id = %0h", data, id);
  endfunction  
endclass

module class_example;
  initial begin
    child_trans c_tr;
    c_tr = new();
    
    c_tr.data = 5;
    c_tr.id = 1;
    c_tr.display();
  end
endmodule

Output:

Child: Value of data = 5 and id = 1

An abstract class with child class handle assignment

In the below example, an object for the child class is created and its handle is assigned to its parent class handle. This is to show that the parent class handle can access its method.

virtual class parent_trans;
  bit [31:0] data;
  int id;
  
  function void display();
     $display("Base: Value of data = %0h and id = %0h", data, id);
  endfunction
endclass

class child_trans extends parent_trans;
  function void display();
    $display("Child: Value of data = %0h and id = %0h", data, id);
  endfunction  
endclass

module class_example;
  initial begin
    parent_trans p_tr;
    child_trans c_tr;
    c_tr = new();
    
    p_tr = c_tr;
    p_tr.data = 5;
    p_tr.id = 1;
    p_tr.display();
  end
endmodule

Output:

Base: Value of data = 5 and id = 1

An abstract class with a virtual method

virtual class parent_trans;
  bit [31:0] data;
  int id;
  
  virtual function void display();
     $display("Base: Value of data = %0h and id = %0h", data, id);
  endfunction
endclass

class child_trans extends parent_trans;
  function void display();
    $display("Child: Value of data = %0h and id = %0h", data, id);
  endfunction  
endclass

module class_example;
  initial begin
    parent_trans p_tr;
    child_trans c_tr;
    c_tr = new();
    
    p_tr = c_tr;
    p_tr.data = 5;
    p_tr.id = 1;
    p_tr.display();
  end
endmodule

Output:

Child: Value of data = 5 and id = 1

An abstract class with pure virtual methods

A method in an abstract class may or may not be overridden by its derived classes. Sometimes, if it is necessary to write a specific method for different derived classes, the pure virtual method is useful. 

A pure virtual method makes it mandatory for methods to be implemented in derived classes whose prototype has been specified in an abstract class. 

  1. The pure virtual method has no implementation mentioned in the base class. It is just a prototype of the method.
  2. A pure virtual method and its implementation in derived class shall use the same signature i.e. same arguments and return type if applicable.

Pure virtual method example

Pure virtual methods include the pure virtual function and the pure virtual task.

virtual class parent_trans;
  bit [31:0] data;
  int id;
  
  pure virtual function void display();
endclass

class child_trans extends parent_trans;
  function void display();
    $display("Child: Value of data = %0h and id = %0h", data, id);
  endfunction  
endclass

module class_example;
  initial begin
    parent_trans p_tr;
    child_trans c_tr;
    c_tr = new();
    
    p_tr = c_tr;
    p_tr.data = 5;
    p_tr.id = 1;
    p_tr.display();
  end
endmodule

Output:

Child: Value of data = 5 and id = 1

System Verilog Tutorials