SystemVerilog OOP Mastery · All levels

Class Inheritance with `extends`: Exercises

Exercises for Class Inheritance with `extends`.

Practice exercises

Exercise 1

Create class dma_txn extending txn_base with fields len and burst_type. Ensure inherited addr is initialized and len defaults to 16.

Solution

diagram
Implement function new(bit [31:0] addr = '0, int unsigned len = 16, bit [1:0] burst_type = 0); call super.new(addr); then assign this.len and this.burst_type.

Exercise 2

Refactor a class hierarchy where crc behavior is inherited from a second pseudo-base into composition.

Solution

diagram
Keep one real base class for transaction identity. Move CRC logic to helper class crc_policy; instantiate crc_policy inside the transaction and delegate CRC methods through that handle.

Related topics