ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
class Base {
    static void show() { System.out.print("Base.static "); }
}
class Derived extends Base {
    static void show() { System.out.print("Derived.static "); } // hides, doesn't override
}
// in main:
Base b = new Derived();
b.show();
  1. A. Derived.static
  2. B. Compilation error: static methods cannot be called via instance reference
  3. C. Compilation error: duplicate show()
  4. D. Base.static

Correct answer: D. Base.static

Static methods are resolved by the reference's STATIC/compile-time type, never by the object's runtime type (they are not polymorphic). b's declared type is Base, so b.show() resolves to Base.show() at compile time, printing "Base.static", even though b actually refers to a Derived object at runtime.

More OOP with C++ and Java questions

Exams that ask this topic