# Difference between dynamic casting and using "virtual" method in base class

**URL:** <https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607>\
**Category:** SystemVerilog\
**Tags:** Polymorhphism, virtual-key-word, SystemVerilog, cast, dynamic-casting\
**Created:** [May 1, 2023, 4:11pm UTC](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607 "2023-05-01T16:11:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harshad](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harshad/32/1775_2.png) [@Harshad](https://verificationacademy.com/forums/u/Harshad)\
**Post date:** [May 1, 2023, 4:11pm UTC](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/1 "2023-05-01T16:11:34Z")

</div>

If we want to access the derived class method using base class handle, we can do this by defining base class method as virtual but while i am looking at the “dynamic casting” concept this is also doing the same thing. so, I am confused whether both are serving same purpose and does both are the example of polymorphism definition?  
Can someone explain the difference between both using simple example?  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 1, 2023, 5:37pm UTC](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/2 "2023-05-01T17:37:14Z")

</div>

_In reply to [Harshad](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607):_

Dynamic casting is not an example polymorphism because it requires prior knowledge of a class extension to perform the cast.

---

<div class="post-metadata">

**Author:** ![Harshad](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harshad/32/1775_2.png) [@Harshad](https://verificationacademy.com/forums/u/Harshad)\
**Post date:** [May 2, 2023, 4:02am UTC](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/3 "2023-05-02T04:02:12Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/2):_

> _In reply to [Harshad](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/2):_  
> Dynamic casting is not an example polymorphism because it requires prior knowledge of a class extension to perform the cast.

Thanks Dave for quick reply!!!, but can you please explain this with some example?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 2, 2023, 5:09am UTC](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/4 "2023-05-02T05:09:26Z")

</div>

_In reply to [Harshad](https://verificationacademy.com/forums/t/difference-between-dynamic-casting-and-using-virtual-method-in-base-class/41607/3):_

Here is an example without virtual methods using dynamic cast. Look specificaly at the code in somefunc. Think about what you would have to do if class A was extended multiple times.

```verilog
module top;
  
class A;
 rand int member1;
 function void printme1;
   $display("member1 ",member1);
 endfunction
endclass
class B extends A;
 int member2;
 rand function void printme2;
   printme1;
   $display("member2 ",member2);
 endfunction
endclass

  function void somefunc(A a_h);
    B b_h;
    void'(a_h.randomize);
    if ($cast(b_h, a_h))
       b_h.printme2;
     else
       a_h.printme1;
  endfunction
  
  A h1 = new;
  B h2 = new;
  initial begin
    somefunc(h1);
    somefunc(h2);
  end
endmodule

```

Here is the same example using a virtual method.

```verilog
class A;
 rand int member1;
 virtual function void printme;
   $display("member1 ",member1);
 endfunction
endclass
  class B extends A;
 rand int member2;
 function void printme;
   super.printme;
   $display("member2 ",member2);
 endfunction
endclass

  function void somefunc(A a_h);
    B b_h;
    void'(a_h.randomize);
    a_h.printme;
  endfunction
  
  A h1 = new;
  B h2 = new;
  initial begin
    somefunc(h1);
    somefunc(h2);
  end
endmodule

```
