You are on page 1of 4

10/29/2014 WWW.TESTBENCH.

IN - Systemverilog OOPS

| HOME | ABOUT | ARTICLES | ACK | FEEDBACK | TOC | LINKS | BLOG | JOBS | Search

TUTORIALS POLYMORPHISM Index


Introduction
SystemVerilog Class
Verification Object
This
Constructs
Polymorphism allows an entity to take a variety of representations. Polymorphism Inheritance
Interface means the ability to request that the same Operations be performed by a wide Encapsulation
OOPS range of different types of things. Effectively, this means that you can ask many Polymorphism
Randomization different objects to perform the same action. Override polymorphism is an override Abstract Classes
of existing code. Subclasses of existing classes are given a "replacement method" for Parameterised Class
Functional Coverage Nested Classes
methods in the superclass. Superclass objects may also use the replacement
Assertion methods when dealing with objects of the subtype. The replacement method that a Constant
DPI subclass provides has exactly the same signature as the original method in the Static
superclass. Casting
UVM Tutorial
Copy
VMM Tutorial Polymorphism allows the redefining of methods for derived classes while enforcing a Scope Resolution
OVM Tutorial common interface.To achieve polymorphism the 'virtual' identifier must be used Operator
Easy Labs : SV when defining the base class and method(s) within that class. Null
External Declaration
Easy Labs : UVM Classes And Structures
Easy Labs : OVM Typedef Class
Easy Labs : VMM EXAMPLE: without virtual Pure
class A ; Other Oops Features
AVM Switch TB
task disp (); Misc
VMM Ethernet sample $display(" This is class A ");
endtask Report a Bug or Comment
endclass on This section - Your
Verilog input is what keeps
Verification class EA extends A ; Testbench.in improving
task disp (); with time!
Verilog Switch TB
$display(" This is Extended class A ");
Basic Constructs endtask
endclass

OpenVera program main ;


Constructs EA my_ea;
A my_a;
Switch TB
RVM Switch TB initial
RVM Ethernet sample begin
my_a = new();
my_a.disp();
Specman E
my_ea = new();
Interview Questions
my_a = my_ea;
my_a.disp();
end
endprogram
RESULTS

This is class A
This is class A

EXAMPLE: with virtual


class A ;
virtual task disp ();
$display(" This is class A ");
endtask
endclass

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:

Member disp not found in class A

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

task just(A my_a);


EA loc;
$cast(loc,my_a);
loc.disp();
endtask

RESULT

This is Extended class A

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

class EA_1 extends A ;


task disp ();
$display(" This is Extended 1 class A ");
endtask
endclass

class EA_2 extends EA_1 ;


task disp ();
$display(" This is Extended 2 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

task just(A my_a);


EA_1 loc;
$cast(loc,my_a);
loc.disp();
endtask

RESULT

This is Extended 2 class A


This is Extended 2 class A

http://www.testbench.in/CL_07_POLYMORPHISM.html 3/4
10/29/2014 WWW.TESTBENCH.IN - Systemverilog OOPS

<< PREVIOUS PAGE TOP NEXT PAGE >>

copyright © 2007-2017 :: all rights reserved www.testbench.in::Disclaimer

http://www.testbench.in/CL_07_POLYMORPHISM.html 4/4

You might also like