When do I need Virtual keyword?

Dear All,

I’m trying to understand the Virtual keyword.

I found a virtual keyword with some keyword such as virtual function, virtual class, virtual interface.
Would you please let me know when do I need that Virtual keyword with example?

for example ,
I came across some example when I googling.




//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

As you can see above example, there was used in class.
But I didn’t get it exactly when I do need it.

In reply to sylee:

That could should have produce an error, you cannot construct a virtual/abstract class.
Please see

https://verificationacademy.com/content/re-use-abstract-class-0

The reason for the virtual keyword instead of using abstract was to keep the number of new keywords down, and virtual tends to mean something that appears but is not quite there.

In reply to dave_59:

Thanks, I thought that the example to show the reason of why we use abstract class.
but not much I can get information from that example, so Would you please give me any snippet code? for understand ?

In reply to sylee:

Have you looked at any of the links I gave you? Especially the first one.

In reply to dave_59:

Of course, I’ve already seen before,

Test1.

virtual class packet;
  bit [31:0] addr;
  
  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends packet;
  function display();
  super.display();
  endfunction
  
endclass

module virtual_class;
  initial begin
    child_class p;
    p = new();
    p.addr = 10;
    p.display(); 
  end
endmodule

VS

Test2


 class packet;
  bit [31:0] addr;
  
   function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends packet;
  function display();
  super.display();
  endfunction
  
endclass

module virtual_class;
  initial begin
    child_class p;
    p = new();
    p.addr = 10;
    p.display(); 
  end
endmodule

I don’t know but I found some resolve solution for my question.
Might be I need to make some derived class by using “extends” when I use “virtual”.
It seems work. but I still didn’t get it exactly why we do use the “virtual” keyword.

One more, When I remove the virtual keyword in Test2 code, but there’s nothing different.

virtual class/abstract class:
if you are declaraing a class as virtual then it means that class can’t be instantiated. virtual class are used to implement any base class which can only be extended. as you have done in your test1 where you define a virtual class and then derived a child class from that base class.

In reply to Mohan Shyam:

virtual class/abstract class:
if you are declaraing a class as virtual then it means that class can’t be instantiated. virtual class are used to implement any base class which can only be extended. as you have done in your test1 where you define a virtual class and then derived a child class from that base class.

Now, I’m getting started to understand about “virtual” keyword. Would you please let me know which part do you indicate about “instantiated” in the Test1 or Test2?

One more, what If I use that “Virtual” to function not class,

class A ; 
    virtual function void disp (); 
      $display(" Non-Virtual from A "); 
    endfunction
    virtual function void vdisp (); 
      $display(" Virtual from A "); 
    endfunction
  endclass 

  class EA extends A ;
     virtual function void disp (); 
      $display(" Non-Virtual from EA "); 
    endfunction
     virtual function void vdisp (); 
      $display(" Virtual from EA "); 
    endfunction
  endclass 
   
module main ; 

  function void disp( EA a);
    a.disp();
    a.vdisp();
  endfunction

  	A my_a; 
	EA my_ea;
  
  
  initial 
  begin 
    my_a = new(); 
    my_ea = new(); 
   
    disp(my_a); 
    disp(my_ea); 
   	
     
  end 
endmodule

What am I supposed to do to use EA not A as the below?

  function void disp( EA a);
    a.disp();
    a.vdisp();
  endfunction