SystemVerilog OOP Mastery · All levels

Interface Classes and `implements`: Exercises

Exercises for Interface Classes and `implements`.

Practice exercises

Exercise 1

Define `interface class printable_if` with `print_line()` and make both `packet_logger` and `error_logger` implement it.

Solution

diagram
Create `printable_if` with one pure virtual method, then add `implements printable_if` to both classes and provide concrete `virtual function void print_line();` implementations.

Exercise 2

Add a new role `timestamped_if` to an existing monitor hierarchy without changing base class ancestry.

Solution

diagram
Create `interface class timestamped_if` and add `implements timestamped_if` to selected monitors. Implement required methods in each class while keeping the same `extends` base.

Related topics