Friday, 28 April 2017

Polymorphism and $cast

Polymorphism 

In OOPS polymorphism means same operation to be performed in wide range. 

-------------------------------------------------------------------------------------
Example: 

class India; 

task my_print();
  $display("This is India");
endtask: my_print

endclass: India

class Kerala extends India; 

task my_print();
  $display("This is Kerala");
endtask: my_print

endclass: Kerala

module top();

India i_var;
Kerala k_var;

initial 
begin
  i_var = new();
  i_var.my_print();
  k_var = new();
  i_var = k_var;
  i_var.my_print();
end

endmodule

Output:
This is India
This is India
-------------------------------------------------------------------------------------

In OOPS, base class can also access the method of child class if the it has the same method defined it it's own class(Here, my_print) and when the base class variable has the child object (Here, i_var = k_var). For that, key is "virtual"

-------------------------------------------------------------------------------------
Example: 

class India; 

virtual task my_print();
  $display("This is India");
endtask: my_print

endclass: India

class Kerala extends India; 

virtual task my_print();
  $display("This is Kerala");
endtask: my_print

endclass: Kerala

module top();

India i_var;
Kerala k_var;

initial 
begin
  i_var = new();
  i_var.my_print();
  k_var = new();
  i_var = k_var;
  i_var.my_print();
end

endmodule

Output:
This is India
This is Kerala
-------------------------------------------------------------------------------------

If method is declared as virtual in the base class, it is still considered as virtual in all the child calss even it is not defined as virtual.

$cast

It is always legal in OOPS, that base class variable has child class object, but it is not true in reverse case. 

-------------------------------------------------------------------------------------
module top();

India i_var;
Kerala k_var;

initial 
begin
  i_var = new();
  k_var = new();
  k_var = i_var;  // Illegal
end

endmodule
-------------------------------------------------------------------------------------

However, base class object can still be assigned to child class, if that base class variable has the child class object. For that $cast is used.

In a simple way, if we want to use the method which is defined inside the child class but not in the parent class, we have to revert back to the variable of child class instead of parent class. 

-------------------------------------------------------------------------------------
Example: 

class India; 
endclass: India

class Kerala extends India; 

virtual task my_print();
  $display("This is Kerala");
endtask: my_print

endclass: Kerala

module top();

India i_var;
Kerala k_var;

initial 
begin
  k_var = new();
  i_var = k_var;
  perform_cast(i_var);
end

task perform_cast(India ind) ;
  Kerala ker;
  $cast(ker, ind);
  ker.my_print();
endtask: perform_cast

endmodule

Output:
This is Kerala
-------------------------------------------------------------------------------------

If parent class variable is not having the object of  child class,
 -------------------------------------------------------------------------------------
Example: 

class India; 
endclass: India

class Kerala extends India; 

virtual task my_print();
  $display("This is Kerala");
endtask: my_print

endclass: Kerala

module top();

India i_var;
Kerala k_var;

initial 
begin
  i_var = new();
  perform_cast(i_var);
end

task perform_cast(India ind) ;
  Kerala ker;
  if (!$cast(ker, ind))
  begin
      $display("This casting is not legal");
  end
  else
  begin
    ker.my_print();
  end
endtask: perform_cast

endmodule

Output:
This casting is not legal
-------------------------------------------------------------------------------------

No comments:

Post a Comment