VLSI DV Interview Puzzles · All levels

Signature Mismatch Prevents Override

Why does base implementation run even though derived defines a method with the same name?

Puzzle

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

Why does base implementation run even though derived defines a method with the same name?

Code

systemverilog
class base_chk;
  virtual 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 = new ext_chk();
  $display("%0d", h.score(5));
end

Hint

Override requires compatible prototype, not just same method name.

Step-by-step solution

diagram
1) `base_chk::score` expects one argument; derived declaration has different signature.
2) This does not override the base virtual slot used by calls through base handle.
3) Call `h.score(5)` resolves to base implementation and prints 6.
4) Fix by matching prototype exactly in derived and handling bonus internally.

Answer

Answer: Because method signatures differ, derived method does not override the base virtual method; base version executes through base handle.

Why candidates get it wrong

Interviewers use this to catch name-based reasoning instead of signature-based override rules.

Interviewer follow-up

Would adding `virtual` keyword on derived method change behavior if signature still mismatches?

Related topics