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:
- Pure Virtual method can only be a prototype or template.
- Pure Virtual method can not have any implementation inside it.
- 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.