ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class A {
    static void m() { System.out.println("A.static"); }
}
class B extends A {
    static void m() { System.out.println("B.static"); }
}
// in main:
A a = new B();
a.m();
  1. A. B.static
  2. B. A.static
  3. C. Compile-time error: static methods cannot be hidden
  4. D. It prints both

Correct answer: B. A.static

Static methods are not polymorphic; they are hidden, not overridden, and resolved using the compile-time (static) type of the reference. a is declared as type A, so a.m() calls A.m() regardless of the runtime type of the object.

More OOP with C++ and Java questions

Exams that ask this topic