🎙 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 1 of 10

What does this print? (code)

What does this print?

class A { String who() { return "A"; } }
class B extends A { String who() { return "B"; } }
public class Main {
    public static void main(String[] args) {
        A obj = new B();
        System.out.println(obj.who());
    }
}
  1. A.A
  2. B.B
  3. C.AB
  4. D.Error
Answer & explanation

Correct answer: B

The object is a B, and overridden methods are chosen at runtime by the object's real type.

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 →