Skip to Dispatch + abstraction questions

Part 2 · Dispatch and abstraction

Trace calls from handle and object types, distinguish overriding from hiding, and use abstract classes and checked casts safely.

Question directory

Questions for Dispatch + abstraction

Open an authored answer, then use the adjacent object diagram or code example to verify the mechanism.

  1. inheritance and dispatchHow does a virtual method choose which implementation runs?SystemVerilog OOP
  2. inheritance and dispatchWhat is the difference between overriding and hiding a method?SystemVerilog OOP
  3. inheritance and dispatchWhat happens when an override calls super.method()?SystemVerilog OOP
  4. inheritance and dispatchWhy do static methods and properties not dispatch polymorphically?SystemVerilog OOP
  5. inheritance and dispatchWhy is calling a virtual method from a base constructor dangerous?SystemVerilog OOP
  6. inheritance and dispatchHow do you solve a mixed virtual and non-virtual polymorphism trace?SystemVerilog OOP
  7. abstract types and castingHow is a virtual class different from a virtual method?SystemVerilog OOP
  8. abstract types and castingWhat does a pure virtual method require from derived classes?SystemVerilog OOP
  9. abstract types and castingWhy is an upcast automatic while a downcast needs $cast()?SystemVerilog OOP
  10. abstract types and castingWhat must match for a derived method to override a virtual method?SystemVerilog OOP
  11. abstract types and castingCan a virtual class handle refer to a concrete derived object?SystemVerilog OOP

SystemVerilog inheritance and dispatchSeparate handle type from object type.

Trace virtual calls, non-virtual hiding, super calls, static method resolution, and the constructor-time dispatch trap without guessing from the variable name.

Concept model

The virtual keyword changes the lookup path

Runtime dispatchThe base declaration marked the method virtual, so the object's dynamic type selects the implementation.

Virtual calls follow the dynamic object type. Non-virtual and static calls are bound from the declared type or class scope, even when the handle refers to a derived object.

Strong answer

When the base declaration is virtual, a call through any compatible handle dispatches from the dynamic type of the object. A base handle referring to a Dog therefore invokes Dog::speak().

Reason it through

  • The handle's declared type controls which members are legal to name at compile time.
  • The object's dynamic type controls which virtual override executes at runtime.
  • Once a method is virtual in a base class, matching overrides remain virtual down the hierarchy.
Upcast, then dispatch dynamicallysystemverilog
class Animal;
  virtual function string speak();
    return "noise";
  endfunction
endclass

class Dog extends Animal;
  function string speak();
    return "woof";
  endfunction
endclass

Dog dog = new;
Animal animal = dog;
$display("%s", animal.speak()); // woof

SystemVerilog abstract types and castingProgram to a contract you can check.

Use virtual classes and pure virtual methods to define incomplete base types, then move through the hierarchy with safe upcasts and checked downcasts.

Concept model

Abstract bases define the contract boundary

Abstract contractDeclare the promise once
cannot instantiatevirtual class Shapepure virtual function area()
concrete childRectarea() implemented
concrete childCirclearea() implemented
Handle conversionUpcast freely; check the way down
Rect handleShape handleupcast · always safe
Shape handleRect handle$cast() · runtime check

A concrete child must satisfy every inherited pure virtual contract. Upcasting preserves the object behind a base handle; $cast() verifies a downcast at runtime.