Skip to content

Virtual Methods

Bhavanasandrala edited this page Sep 21, 2022 · 8 revisions

Virtual Methods

SystemVerilog Methods declared with the keyword virtual are referred to as virtual methods.

virtual

Virtual Methods are two types
1.Virtual Functions
2.Virtual Tasks

Virtual Function

A function declared with a virtual keyword before the function keyword is referred to as virtual Function, we use the virtual in base(parent)class function then only child class gets executed.

Code Snippet

class packet;
string a;
int b;

function new();
a="Team";
b=4;
endfunction

virtual function void  display();
$display("a=%0d",a);
$display("b=%0d",b);
endfunction
endclass
module example;
 initial begin
 p2=new();
 p3=new();
 pp0=p2;
 pp1=new p3;
 //p1.display();
 pp0.display();
 pp1.display();
 //p3.e=7;
 //pp1.display();
 end
endmodule

Output

virtual-function

Virtual Task

Task declared with a virtual keyword before the task keyword is referred to as virtual task