This question might be applicable to all/most Object-oriented programming languages but I am only concerned about the SystemVerilog side. Polymorphism, I thought I understood, until I was talking to a colleague the other day and this came and I realised that I don't know how this can be done. Following are the two classes to consider for the purpose of this discussion -
class four_wheeler;
virtual function void vehicle_type();
$display("Four-wheeler");
endfunction:vehicle_type
virtual task colour();
$display("Many colours");
endtask:colour
endclass:four_wheeler
class jaguar extends four_wheeler;
function void vehicle_type();
$display("Jaguar");
endfunction:vehicle_type
task colour();
$display("Black");
endtask:colour
endclass:jaguar
program sv_prog;
initial begin
four_wheeler four_wheeler_h;
jaguar jaguar_h;
four_wheeler_h = new();
jaguar_h = new();
four_wheeler_h = jaguar_h;
four_wheeler_h.vehicle_type();
end
endprogram: sv_prog
I want to access the vehicle_type() function in the base class four_wheeler using its own object. I can do this by four_wheeler_h.vehicle_type() before the four_wheeler_h = jaguar_h copy. Regular OOP working! But can I do it after the copy of the handle?
I can use the super keyword in jaguar class vehicle_type() method:
function void vehicle_type();
super.vehicle_type();
$display("Jaguar");
endfunction:vehicle_type
and get the output:
Four-wheeler
Jaguar
But I am more interested in doing this from the program block itself without having to modify the function in the jaguar class. Is there any way I can achieve that? Maybe a use of super from the program block itself.