Friday, 5 July 2013

System Verilog Polymorphism

                  
As a fresher coming from Electronics & Communication branch, it was very difficult for me to work directly on System Verilog especially, I don’t know OOPs concepts. Encapsulation, Inheritance and Polymorphism are the most commonly used in System Verilog.

Polymorphism allows the use of a variable of the superclass type to hold subclass objects and to reference the methods of those subclasses directly from the superclass variable. So, you can say Polymorphism = Virtual Methods + Inheritance.

Virtual method overrides in subclasses shall have matching argument types, identical argument names, identical qualifiers, and identical directions to the prototype.

The virtual qualifier is optional in the derived class method declarations. So, if any method declared as virtual in the base class then by default same method works as virtual in child class.

In below example, Animal is a top class, Dog class is derived from Animal class and Puppy class is derived from Dog class. In Animal class which is a base class, eat() method is declared as a virtual method. In derived class Dog, same method is implemented but it is not declared as virtual.

Example:
module top;

   class Animal;
       virtual function eat();
         $display("Animal eat");
       endfunction
   endclass
 
  class Dog extends Animal;
       function eat();
         $display("Dog eat");
       endfunction
  endclass

  class Puppy extends Dog;
       function eat();
         $display("Puppy eat");
       endfunction
  endclass

  initial begin
  Animal a_handle;
  Dog d_handle;
  Puppy p_handle;

  a_handle = new();
  d_handle= new();
  p_handle= new();

  a_handle.eat(); 
  d_handle.eat(); 
  p_handle.eat(); 

  a_handle = d_handle;
  a_handle.eat(); 
  d_handle.eat(); 
  p_handle.eat(); 

  a_handle = p_handle;
  a_handle.eat(); 
  d_handle.eat(); 
  p_handle.eat(); 

  d_handle = p_handle;
  a_handle.eat(); 
  d_handle.eat(); 
  p_handle.eat(); 
end
endmodule

Output:
Animal eat 
Dog eat
Puppy eat  

Dog eat
Dog eat
Puppy eat
 
Puppy eat
Dog eat
Puppy eat

Puppy eat  

Puppy eat 
Puppy eat  
 
As shown above, while calling eat() method of Animal, Dog and Puppy class, respective method is called from respective class. But when handle of Dog class instance is passed to Animal class instance then eat() method of Dog class is called while calling eat() method of Animal class(a_handle.eat()).

Same way, after assigning handle of Puppy class to instance of Animal class, eat() method of Puppy class is called while calling eat() method of Animal class (a_handle.eat()).

Now, if handle of Puppy class is assigned to Dog class instance then eat() method of Puppy class is called every time when eat() method of any class (Animal or Dog or Puppy) is called. Here, while calling eat() method of Animal class, eat() method of Puppy class is called. The reason behind it is, previously, p_handle is assigned with the a_handle.

As shown in the above example, eat() method is not virtual in Dog class but it works as virtual because same method is defined as virtual in it’s parent class.



Reference:
(1) IEEE Standard for SystemVerilog— Unified Hardware Design, Specification, and Verification Language,IEEE Std 1800™-2012 

13 comments:

  1. Good example to clear concepts of polymorphism and inheritance. Great work munjal..

    ReplyDelete
    Replies
    1. in this article,
      d_handle = p_handle;
      a_handle.eat();
      d_handle.eat();
      p_handle.eat();

      answer should be
      animal eat;//you wrote puppy eat....
      Puppy eat
      Puppy eat

      if iam wrong correct me...
      thankyou

      Delete
    2. Here, before assigning p_handle to d_handle, "p_handle" is assigned to "a_handle". That means, a_handle is still pointing to Puppy class. So it prints "Puppy Eat" instead of "Animal Eat".

      Delete
  2. Good article on virtual method and polymorphism concept. Congratulations for your first article.

    ReplyDelete
  3. hi can u give some brief on shallow copy and deep copy

    ReplyDelete
    Replies
    1. In “shallow copy”, object handle will be copied to new location i.e. if older object is changed then data in copied object will be affected while in “deep copy” object would be copied to the new location in which copied object will not be affected while changing the older object.

      Delete
  4. hi can you give clear explanation on a_handle=p_handle.
    a_handle is grand parent and p_handle is grand child can grand child can access its grand parent that is a_handle please clarify this

    ReplyDelete
    Replies
    1. Yes, in that case method of "Puppy" class is called while calling virtual methods of "Animal" class.

      Delete
  5. Very good Example. Loved it. Clear understanding of Polymorphism

    ReplyDelete
  6. Understood polymorphism in system verilog completely

    ReplyDelete