← Quiz: Inheritance and Polymorphism: all 10 questions
Quiz: Inheritance and Polymorphism · Question 8 of 10
What does this print? (code)
What does this print?
class A { static String hello() { return "A"; } }
class B extends A { static String hello() { return "B"; } }
public class Main {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.hello());
}
}
- A.B
- B.Compile error
- C.AB
- D.A
Answer & explanation
Correct answer: A
Static methods are not overridden; B's method only hides A's. The call is decided by the declared type A at compile time.
Can you get the rest right without peeking?
Take the timed Quiz: Inheritance and Polymorphism quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →