advancedCore Java Fundamentals196 of 436
What is the difference between composition and inheritance? When would you prefer one over the other?
Tests whether you default to 'favor composition over inheritance' for the right reason -- composition keeps classes loosely coupled and avoids the fragile base class problem, while inheritance is justified only for genuine is-a relationships where subtypes must be substitutable for the base.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response