Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
    • Handle and Copy Puzzles
    • Virtual Method Puzzles
      • Non-Virtual vs Virtual Through Base Handle
      • Missing super.new with Required Base Args
      • Virtual Call from Base Constructor
      • Same Name, Different Prototype: Hiding Not Overriding
      • Template Method with Non-Virtual Wrapper
      • super Method Call in Override Chain
    • Parameterized Class Puzzles
    • Factory Override Puzzles
    • OOP & Testbench Puzzles: Tricky Q&A
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←Virtual Method Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. OOP & Testbench Puzzles
  4. Virtual Method Puzzles
  5. Same Name, Different Prototype: Hiding Not Overriding
On this page

    VLSI DV Interview PuzzlesAll levels

    Same Name, Different Prototype: Hiding Not Overriding

    A derived class adds a defaulted argument to a base method of the same name. Predict both prints, and say what changes if the base method were declared `virtual`.

    Puzzle

    Difficulty: Medium · Puzzle 4 of 6 · Topic: Virtual Method Puzzles

    A derived class adds a defaulted argument to a base method of the same name. Predict both prints, and say what changes if the base method were declared `virtual`.

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    class base_chk;
      function int score(int e);
        return e + 1;
      endfunction
    endclass
    
    class ext_chk extends base_chk;
      function int score(int e, int bonus = 10);
        return e + bonus;
      endfunction
    endclass
    
    initial begin
      base_chk h;
      ext_chk  x;
      x = new();
      h = x;
      $display("via base=%0d via ext=%0d", h.score(5), x.score(5));
    end

    Hint

    Only a virtual base method lets the runtime object type pick the implementation. Without it, the handle type picks.

    Step-by-step solution

    Diagram
    1) `base_chk::score` is non-virtual, so there is no override relationship; `ext_chk::score` is an unrelated method that hides the base name for `ext_chk` handles.
    2) `h.score(5)` binds statically to `base_chk::score`: 5 + 1 = 6.
    3) `x.score(5)` binds to `ext_chk::score(5, 10)`: 5 + 10 = 15.
    4) If `base_chk::score` were `virtual`, the extra argument makes the derived prototype mismatch the virtual one. IEEE 1800 8.20 requires an exact match, so that is a compile error in a conforming tool (some tools are lenient), not silent hiding.

    Answer

    Answer: Prints `via base=6 via ext=15`. The base method is non-virtual, so each call binds to its handle type. Making the base method virtual turns the same derived declaration into an illegal prototype mismatch.

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. The shown code run in Verilator: a non-virtual same-name method is hidden, not overridden.

    Why candidates get it wrong

    Same name does not mean override. Whether the code is silently wrong (non-virtual: two different methods) or rejected (virtual: mismatched prototype) depends only on the base keyword; never rely on a tool's leniency to "just run the base version".

    Interviewer follow-up

    Would adding `virtual` on the derived method alone make `h.score(5)` print 15? Model answer: no. A `virtual` on the derived side only starts a new virtual method for `ext_chk` and below; `h` is a `base_chk` handle and `base_chk::score` stays non-virtual, so `h.score(5)` is still 6. Dispatch is decided by the base declaration the handle sees.

    Say it out loud

    It prints via base equals 6 and via ext equals 15. The base method is non-virtual, so this is hiding, not overriding, and each call binds to its handle type. The base handle runs the one-argument version and the extended handle runs the two-argument version. The trap is that if the base method were virtual, the different prototype would be a compile error, so I would check the virtual keyword and the prototype.

    Related topics

    • Virtual dispatch

    • extends and super

    • Back to Virtual Method Puzzles

    • OOP & Testbench Puzzles section

    Previous

    45 / 293

    Next: Template Method with Non-Virtual Wrapper

    Up next

    Up nextTemplate Method with Non-Virtual Wrapper

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Non-Virtual vs Virtual Through Base Handle
    • Missing super.new with Required Base Args
    • super Method Call in Override Chain