ProgrammingHardIBM · Deloitte pattern

Which statement is correct?

Which statement is correct?
class Event {
    protected void log() { System.out.print("Event.log"); }
}
class ClickEvent extends Event {
    void log(String tag) { System.out.print("ClickEvent.log " + tag); }
}
public class Main {
    public static void main(String[] args) {
        ClickEvent c = new ClickEvent();
        c.log();
    }
}
  1. A. It fails to compile: log() is ambiguous between the two
  2. B. It prints 'ClickEvent.log '
  3. C. It prints 'Event.log' — log(String) is an overload, not an override, so the no-arg call resolves to the inherited method
  4. D. It fails to compile: log(String) must call log() internally

Correct answer: C. It prints 'Event.log' — log(String) is an overload, not an override, so the no-arg call resolves to the inherited method

log(String) in ClickEvent has a different parameter list from Event's log(), so it's an unrelated overload, not an override — Event.log() remains intact and reachable. c.log() with no arguments resolves to the inherited no-arg method, printing 'Event.log'.

More OOP with C++ and Java questions

Exams that ask this topic