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