← 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?
- A.
class FlyingSwimmer extends Flyer, Swimmer - B.A deep inheritance chain with one class per combination
- C.A character that holds a list of Ability objects (composition)
- 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 →