# Casting a class multiple levels - possible?

**URL:** <https://verificationacademy.com/forums/t/casting-a-class-multiple-levels-possible/30834>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, class, cast\
**Created:** [July 6, 2015, 8:48pm UTC](https://verificationacademy.com/forums/t/casting-a-class-multiple-levels-possible/30834 "2015-07-06T20:48:12Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [July 6, 2015, 9:04pm UTC](https://verificationacademy.com/forums/t/casting-a-class-multiple-levels-possible/30834/2 "2015-07-06T21:04:45Z")

</div>

Certainly. But it will only work if the object is being cast is indeed a derive2 class.  
Here is a complete self-contained example, with code tags. Please show your code this way so it is easy for others to give it a try.

```verilog
module prg;
class base;
  int x;
endclass

class derive1 extends base;
  int y;
endclass

class derive2 extends derive1;
  int z;
endclass
base b1,b;
derive2 d2,d;
initial begin
   d = new();
   b1 = d; // no $cast needed
   $cast(d2,b1); // $cast needed, and will succeed
   b1 = new;
   $cast(d2,b1); // $cast needed, and will fail
  end
endmodule

```

---

_[View the full topic](https://verificationacademy.com/forums/t/casting-a-class-multiple-levels-possible/30834)._
