Virtual Vs Pure Virtual Methods:

Before going towards Virtual and Pure Virtual methods let’s understand Virtual Class (Abstract Class) to make it more clear the understanding of exact concepts.

Virtual Class (Abstract Class): Abstract class is nothing but a class that can be extended but cannot be instantiated. It’s the intention to be only a base class or prototype class.

Virtual Method: It’s a method that can be inherited in a derived class and you can override its behavior. If an object of that derived class is accessed using a handle to its base class, the function call is performed polymorphically – the function called is determined by the type of the object pointed to, not the type of the handle (so the overridden function is called if the object pointed to is of the derived class type).

Pure Virtual Method: It is a virtual method declared in an abstract class (Virtual Class) to make sure that a derived class must have its implementation. These are mentioned as pure as implementation would differ based on derived classes but are a must.

Pure Virtual Methods having the following restrictions:

  1. Pure Virtual method can only be a prototype or template.
  2. Pure Virtual method can not have any implementation inside it.
  3. It is even not allowed “end” keyword for example endfunction/endtask.

Here is the example

virtual class A;
  bit [7:0] a;
  pure virtual function void set(bit [7:0] b);
endclass
 
class B extends A;
  virtual function void set(bit [7:0] b);
    a = b;
  endfunction
endclass

In the above example, you can notice that I declared pure virtual function set in an Abstract Class(Virtual Class) and Overridden in my derived class which is extended from Class A.

About the author

The Art of Verification

Hi, I’m Hardik, and welcome to The Art of Verification.

I’m a Verification Engineer who loves to crack complex designs and here to help others commit to mastering Verification Skills through self-learning, System Verilog, UVM, and most important to develop that thought process that every verification engineer should have.

I’ve made it my mission to give back and serve others beyond myself.

I will NEVER settle for less than I can be, do, give, or create.

View all posts