ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class A {
    final String tag() { return "A"; }
}
class B extends A {
    String tag(int x) { return "B" + x; }
}
// in main:
A a = new B();
System.out.println(a.tag());
  1. A. A
  2. B. B
  3. C. Compilation error: tag() cannot be hidden
  4. D. Compilation error: B must override tag()

Correct answer: A. A

A.tag() is final, so B cannot override it (B.tag(int) is a different overload, not an override attempt, since it has a parameter). a.tag() always resolves to A's own final implementation, printing "A".

More OOP with C++ and Java questions

Exams that ask this topic