Difficulty: Easy · Puzzle 1 of 6 · Topic: Virtual Method Puzzles
A framework stores derived objects in base handles. Predict output and explain why only one method dispatches to derived.
class checker_base;
function string mode();
return "BASE";
endfunction
virtual function string detail();
return "base-detail";
endfunction
endclass
class checker_ext extends checker_base;
function string mode();
return "EXT";
endfunction
function string detail();
return "ext-detail";
endfunction
endclass
initial begin
checker_base c;
c = new checker_ext();
$display("mode=%s detail=%s", c.mode(), c.detail());
endDispatch behavior is determined by virtual-ness in the base declaration.
1) `mode()` is non-virtual in base, so call binds statically to `checker_base::mode`.
2) `detail()` is virtual in base, so call dispatches dynamically to `checker_ext::detail`.
3) Output becomes `mode=BASE detail=ext-detail`.Answer: Output is `mode=BASE detail=ext-detail`; non-virtual methods bind to static handle type while virtual methods use runtime object type.
Declaring method in derived does not create polymorphism unless base declaration is virtual.
If `mode` is made virtual only in derived, does behavior change through `checker_base` handle?