← Quiz: Composition vs Inheritance: all 10 questions
Quiz: Composition vs Inheritance · Question 4 of 10
What does this print? (code)
What does this print?
class Base {
void hello() { System.out.print("Base "); greet(); }
void greet() { System.out.print("hi"); }
}
class Child extends Base {
@Override
void greet() { System.out.print("hello"); }
}
public class Main {
public static void main(String[] args) {
new Child().hello();
System.out.println();
}
}
- A.Base hi
- B.Base hello
- C.hello
- D.Base
Answer & explanation
Correct answer: Base hello
hello() is inherited, but its internal call to greet() runs the overridden version — the same mechanism behind the CountingSet bug.
Can you get the rest right without peeking?
Take the timed Quiz: Composition vs Inheritance quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →