← 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());
}
}
- A.A
- B.B
- C.AB
- 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 →