Professional Documents
Culture Documents
IN - Systemverilog OOPS
| HOME | ABOUT | ARTICLES | ACK | FEEDBACK | TOC | LINKS | BLOG | JOBS | Search
This is class A
This is class A
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
http://www.testbench.in/CL_07_POLYMORPHISM.html 1/4
10/29/2014 WWW.TESTBENCH.IN - Systemverilog OOPS
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_a = new();
my_a.disp();
my_ea = new();
my_a = my_ea;
my_a.disp();
end
endprogram
RESULTS
This is class A
This is Extended class A
Observe the above two outputs. Methods which are declared as virtual are
executing the code in the object which is created.
The methods which are added in the subclasses which are not in the parent class
canot be acessed using the parent class handle. This will result in a compilation
error. The compiler check whether the method is exesting the parent class
definition or not.
EXAMPLE:
class A ;
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_ea = new();
my_a = my_ea;
my_ea.disp();
my_a.disp();
end
endprogram
RESULT:
To access the varible or method which are only in the subclass and not in the
parent class, revert back the object to the subclass handle.
EXAMPLE:
class A ;
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
http://www.testbench.in/CL_07_POLYMORPHISM.html 2/4
10/29/2014 WWW.TESTBENCH.IN - Systemverilog OOPS
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_ea = new();
my_a = my_ea;
just(my_a);
end
endprogram
RESULT
Let us see one more example, A parent class is extended and virtual method is
redefined in the subclass as non virtual method. Now if furthur extention is done to
the class, then the method is still considered as virtual method and Polymorphism
can be achived still. It is advised to declare a method as virtual in all its subclass, if
it is declared as virtual in baseclass , to avoid confusion to the end user who is
extend the class.
EXAMPLE:
class A ;
virtual task disp ();
$display(" This is class A ");
endtask
endclass
program main ;
EA_2 my_ea;
EA_1 my_a;
initial
begin
my_ea = new();
my_a = my_ea;
my_a.disp();
just(my_a);
end
endprogram
RESULT
http://www.testbench.in/CL_07_POLYMORPHISM.html 3/4
10/29/2014 WWW.TESTBENCH.IN - Systemverilog OOPS
http://www.testbench.in/CL_07_POLYMORPHISM.html 4/4