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

A game character needs to fly AND swim, and abilities can change during the game. Which design fits best?

A game character needs to fly AND swim, and abilities can change during the game. Which design fits best?

  1. A.class FlyingSwimmer extends Flyer, Swimmer
  2. B.A deep inheritance chain with one class per combination
  3. C.A character that holds a list of Ability objects (composition)
  4. D.Copy-paste the fly and swim code into the character class
Answer & explanation

Correct answer: A character that holds a list of Ability objects (composition)

Composition combines behaviours and allows changing them at runtime; Java has no multiple class inheritance.

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 →