Part 2 · OOP for Verification · Intermediate
Inheritance & Polymorphism
Hub — extends/super, virtual dispatch, $cast, abstract and interface classes, polymorphic containers, and interview traps.
Overview
Inheritance lets a class extend another, reusing its properties and methods while adding or overriding behavior. Polymorphism is the payoff: code written against a base-class handle works unchanged when handed a derived object — provided the methods involved are virtual. This pair of features is what makes testbench reuse possible: a generic driver drives any transaction derived from the base, a scoreboard compares any derived type through virtual compare(), and tests swap in error-injecting transactions without touching the environment. It is also the densest interview territory in SystemVerilog — dispatch rules, casting, and construction order all hide subtle traps.
Sub-topics
extends & super — derived classes, overriding, base bus_txn → axi_txn example.
Virtual Methods & Dynamic Dispatch — declared-type vs object-type resolution, dispatch-table mental model.
Upcasting, Downcasting & $cast — implicit upcast, runtime-checked downcast, failure handling.
Abstract Classes & Interface Classes — virtual class, pure virtual methods, implements contracts.
Polymorphic Containers — base-handle queues holding mixed derived objects, factory creation.
Inheritance Interview Traps — five classic puzzles with worked answers.
INHERITANCE & POLYMORPHISM — topic map
virtual class txn_base ← 4. abstract contract
│ extends
┌────┴─────┐
bus_txn irq_txn
│ extends
axi_txn ← 1. extends & super
base handle ──► derived object
│
├─ b.calc() virtual? object type wins ← 2. dynamic dispatch
├─ $cast(d, b) checked downcast ← 3. casting
├─ txn_base q[$] mixed types in one queue← 5. containers
└─ traps: non-virtual / hidden props /
ctor order / sibling cast ← 6. interview trapsKey takeaways
extends reuses and specializes; virtual methods make base-handle code dispatch to derived behavior.
Upcasts are implicit and safe; downcasts need $cast and a runtime check.
Polymorphic containers plus virtual methods are the backbone of every reusable verification environment.