Object-Oriented Programming · Question 7 · Hard

What is the output of the following Java code?

class A {
    static void show() { System.out.println("A"); }
}
class B extends A {
    static void show() { System.out.println("B"); }
}
// in main:
A obj = new B();
obj.show();
  1. A
  2. B
  3. Compilation error
  4. Runtime error
Answer and explanation

Answer: A. A

Explanation: Static methods are not overridden; they are hidden. A static call is resolved at compile time from the reference type (A), so A.show() runs and prints "A".

Previous: What is the output of the following Java code? · Next: Which statement about abstract classes and interfaces in Java is correct?

Practise object-oriented programming

Try a fresh set of object-oriented programming questions with an explanation after every answer, free and without signing up: Object-Oriented Programming practice. Or sit a full placement mock test.

More object-oriented programming questions

More technical & programming practice

DBMS · SQL · Operating Systems · Computer Networks · Software Engineering · Cloud & DevOps · GenAI Basics · Pseudo-code

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge