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


Tutorials



POLYMORPHISM




Polymorphism allows an entity to take a variety of representations. Polymorphism means the ability to request that the same Operations be performed by a wide range of different types of things. Effectively, this means that you can ask many different objects to perform the same action. Override polymorphism is an override of existing code. Subclasses of existing classes are given a "replacement method" for methods in the superclass. Superclass objects may also use the replacement methods when dealing with objects of the subtype. The replacement method that a subclass provides has exactly the same signature as the original method in the superclass.

Polymorphism allows the redefining of methods for derived classes while enforcing a common interface.To achieve polymorphism the 'virtual' identifier must be used when defining the base class and method(s) within that class.



EXAMPLE: without virtual
class A ;
task disp ();
$display(" This is class A ");
endtask
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_a = new();
my_a.disp();

my_ea = new();
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
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
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

Index
Introduction
Class
Object
This
Inheritance
Encapsulation
Polymorphism
Abstract Classes
Parameterised Class
Nested Classes
Constant
Static
Casting
Copy
Scope Resolution Operator
Null
External Declaration
Classes And Structures
Typedef Class
Pure
Other Oops Features
Misc

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

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