🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
    }
}
  1. A.B
  2. B.Compile error
  3. C.AB
  4. 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 →