# Extending parameterized classes

**URL:** <https://verificationacademy.com/forums/t/extending-parameterized-classes/44710>\
**Category:** SystemVerilog\
**Tags:** Inheritance, ParameterizedClass, Virtual-methods\
**Created:** [January 4, 2024, 7:44pm UTC](https://verificationacademy.com/forums/t/extending-parameterized-classes/44710 "2024-01-04T19:44:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kpdeepak](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@kpdeepak](https://verificationacademy.com/forums/u/kpdeepak)\
**Post date:** [January 4, 2024, 7:44pm UTC](https://verificationacademy.com/forums/t/extending-parameterized-classes/44710/1 "2024-01-04T19:44:46Z")

</div>

Hi All,

In the following example,

```auto
class Trans #(int awidth=8, dwidth=8);
    string name;
    function new(string name = "");
        this.name = name;
    endfunction
    virtual function void display();
        $display("name = %s, awidth=%0d, dwidth=%0d",this.name,awidth,dwidth);
    endfunction
endclass

class Trans16#(int awidth=16, dwidth=16) extends Trans;
    function new(string name = "");
        super.new(name);
    endfunction
endclass

module tb_top();
Trans t1,t2;
Trans16 t16_1,t16_2;
initial begin
    t1 = new("t1");
    t16_1 = new("t16_1");
    t1.display();
    t16_1.display();

end
endmodule

```

Here t16\_1.display() function gives 8 not 16. Why does it not give 16?  
I think Im missing some fundamental concepts here. Appreciate any/all comments.

---

<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:** [January 4, 2024, 8:05pm UTC](https://verificationacademy.com/forums/t/extending-parameterized-classes/44710/2 "2024-01-04T20:05:44Z")

</div>

Your problem is you did not pass your parameter values down to the base `class Trans`. Try writing your classes like:

```auto
class Trans #(int awidth, dwidth); // removing defaults forces you to provide overrides
    string name;
    function new(string name = "");
        this.name = name;
    endfunction
    virtual function void display();
        $display("name = %s, awidth=%0d, dwidth=%0d",this.name,awidth,dwidth);
    endfunction
endclass

class Trans16#(int awidth=16, dwidth=16) extends Trans#(awidth,dwidth);
    function new(string name = "");
        super.new(name);
    endfunction
endclass

```

---

<div class="post-metadata">

**Author:** ![kpdeepak](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@kpdeepak](https://verificationacademy.com/forums/u/kpdeepak)\
**Post date:** [January 5, 2024, 5:35am UTC](https://verificationacademy.com/forums/t/extending-parameterized-classes/44710/3 "2024-01-05T05:35:11Z")

</div>

Thanks @dave_59 . That solved the problem.

Also is there any way/method we can use the handle of the base\_class (8,8) point to a derived class with a different parameter like (16,16)?

---

<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:** [January 5, 2024, 7:16am UTC](https://verificationacademy.com/forums/t/extending-parameterized-classes/44710/4 "2024-01-05T07:16:16Z")

</div>

It all depends on what you are trying to accomplish. There is nothing saying you _have to_ link the derived class parameters to the to the base class parameters. But if the base class needs the derived parameter values, then it becomes a different incompatible class type. What people sometimes do in that case is create another lower level base class without parameterizations.

```auto
 virtual class base_trans; // an abstract class that never gets constructed. 
    pure virtual function void display;
 endclass
 class Trans #(int awidth, dwidth) extends base_trans;
 ...
 endclass
class DerivedTrans#(int awidth, dwidth) extends Trans#(awidth,dwidth);
...
endclass
base_trans list[$];
DerivedTrans#(16,16) trans16 = new("trans16");
DerivedTrans#(32,32) trans32 = new("trans32");
list.push_back(trans16);
list.push_back(trans32);
foreach(list[i]) list.display();

```

---

<div class="post-metadata">

**Author:** ![kpdeepak](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/8797f3/32.png) [@kpdeepak](https://verificationacademy.com/forums/u/kpdeepak)\
**Post date:** [January 5, 2024, 5:17pm UTC](https://verificationacademy.com/forums/t/extending-parameterized-classes/44710/5 "2024-01-05T17:17:43Z")

</div>

Thanks a lot @dave_59 That did the trick.
