SystemVerilog OOP Mastery · All levels

No Multiple Inheritance (Only Interface-Class Contracts): Exercises

Exercises for No Multiple Inheritance (Only Interface-Class Contracts).

Practice exercises

Exercise 1

Refactor a class that currently extends an overly broad base just to reuse logging. Keep one meaningful base class and pull logging into a composed helper while preserving polymorphic calls.

Solution

diagram
Introduce a small logger_helper class, keep the original semantic base in extends, add an interface class for logging contract, and delegate logging calls through the helper.

Exercise 2

Design a transaction class that must be comparable and serializable without multiple inheritance.

Solution

diagram
Use one extends chain (for lifecycle), implement comparable_if and serializable_if contracts, and compose compare_helper/serialize_helper utilities to centralize shared logic.

Related topics