In reply to dave_59:
Could someone help explain this behavior difference? Shouldn’t functions that are virtual be always virtual? Does it matter where it is made virtual-parent/child class.
Variant 1.
module test;
class A;
virtual function void showMsg(int val);
$display("This is A");
endfunction
endclass
class B extends A;
function void showMsg(int val);
$display("This is B, val = %0d", val);
endfunction
endclass
class C extends B;
function void showMsg(int val);
$display("This is C, val=%0d", val+1);
endfunction
endclass
initial begin
A a;
B b;
C c;
b = new();
a = b;
a.showMsg(1);
c = new();
b = c;
b.showMsg(7);
end
endmodule
Result-
This is B, val = 1
This is C, val=8
Variant 2:
module test;
class A;
function void showMsg(int val);
$display("This is A");
endfunction
endclass
class B extends A;
virtual function void showMsg(int val);
$display("This is B, val = %0d", val);
endfunction
endclass
class C extends B;
function void showMsg(int val);
$display("This is C, val=%0d", val+1);
endfunction
endclass
initial begin
A a;
B b;
C c;
b = new();
a = b;
a.showMsg(1);
c = new();
b = c;
b.showMsg(7);
end
endmodule
Result -
This is A
This is C, val=8
The only difference between the two is the place where virtual keyword is used. Rest of the method prototype being exactly the same.