Composition vs Inheritance Interview Questions and Answers
Master Composition vs Inheritance with production-ready interview questions covering HAS-A vs IS-A relationships, code reuse, loose coupling, flexibility, delegation, SOLID principles, and enterprise Java design examples.
Composition vs Inheritance Interview Questions & Answers
Introduction
One of the most common Senior Java interview questions is:
"Should we use Composition or Inheritance?"
Many developers initially use inheritance for code reuse, but experienced architects often prefer Composition because it provides:
- Loose Coupling
- Better Flexibility
- Easier Testing
- Better Maintainability
Modern enterprise frameworks like:
- Spring Boot
- Hibernate
- Spring Security
heavily favor Composition over Inheritance.
Understanding the trade-offs between these two approaches is essential for writing scalable Java applications.
1. What is Inheritance?
Answer
Inheritance establishes an IS-A relationship between two classes.
Example
class Animal {
void eat() {
}
}
class Dog extends Animal {
}
Illustration
Animal
│
extends
│
▼
Dog
Dog inherits the behavior of Animal.
2. What is Composition?
Answer
Composition establishes a HAS-A relationship.
Instead of inheriting behavior,
one object contains another object.
Example
class Engine {
}
class Car {
private Engine engine =
new Engine();
}
Illustration
Car
│
HAS-A
│
▼
Engine
The Car object delegates engine-related work to the Engine object.
3. What is the difference between Composition and Inheritance?
Answer
| Composition | Inheritance |
|---|---|
| HAS-A relationship | IS-A relationship |
| Loose coupling | Tight coupling |
| More flexible | Less flexible |
| Runtime behavior can be changed | Behavior fixed by hierarchy |
| Delegation | Reuse through inheritance |
| Preferred in modern design | Use only for true IS-A relationships |
Composition generally provides greater long-term flexibility.
4. What is a HAS-A relationship?
Answer
HAS-A means one object contains or uses another object.
Examples
Car HAS-A Engine
Order HAS-A Customer
Laptop HAS-A Battery
Employee HAS-A Address
Composition models ownership or usage relationships.
5. What is an IS-A relationship?
Answer
IS-A means one class is a specialized version of another.
Examples
Dog IS-A Animal
Car IS-A Vehicle
SavingsAccount IS-A BankAccount
If the statement does not naturally read as "IS-A",
inheritance is usually inappropriate.
6. Why is Composition preferred over Inheritance?
Answer
Composition provides:
- Loose coupling
- Better testability
- Easier replacement of implementations
- Greater flexibility
- Smaller class hierarchies
Instead of changing parent classes,
behavior can be replaced simply by changing the composed object.
7. What is Delegation?
Answer
Delegation means an object forwards work to another object.
Example
class Car {
private Engine engine =
new Engine();
void start() {
engine.start();
}
}
Execution
Car
↓
Engine.start()
The Car object delegates engine functionality to Engine.
8. Can Composition replace Inheritance?
Answer
In many cases, yes.
Instead of
Vehicle
↓
ElectricCar
↓
SportsElectricCar
↓
LuxurySportsElectricCar
Use
Car
│
├── Engine
├── Transmission
├── Navigation
├── Battery
└── MusicSystem
This produces a simpler and more maintainable design.
9. When should Inheritance be used?
Answer
Use inheritance when:
- There is a true IS-A relationship.
- Behavior should naturally be shared.
- Subclasses satisfy the Liskov Substitution Principle.
- Parent-child relationships are stable.
Examples
Exception
↓
RuntimeException
↓
IllegalArgumentException
This is a good inheritance hierarchy.
10. When should Composition be used?
Answer
Use composition when:
- Behavior may change.
- Objects collaborate.
- Multiple capabilities are required.
- Flexibility is important.
- Code should remain loosely coupled.
Example
OrderService
↓
PaymentService
↓
NotificationService
↓
InventoryService
Each service collaborates rather than inherits.
11. Explain a production use case.
Answer
Scenario
A Spring Boot payment application.
PaymentController
│
▼
PaymentService
│
┌──────┼────────────┐
│ │ │
▼ ▼ ▼
Gateway Logger Notification
Instead of inheriting logging or notification behavior,
the service composes specialized components.
Benefits
- Independent testing
- Easier replacement
- Better maintainability
- Loose coupling
12. What are common design mistakes?
Answer
Common mistakes include:
Using inheritance only for code reuse.
Creating deep inheritance hierarchies.
Violating the Liskov Substitution Principle.
Creating God parent classes.
Ignoring composition.
Large inheritance trees become difficult to maintain.
13. What are the best practices?
Answer
Recommended practices
- Prefer composition over inheritance.
- Use inheritance only for true IS-A relationships.
- Keep inheritance hierarchies shallow.
- Program to interfaces.
- Delegate behavior to collaborating objects.
- Follow SOLID principles.
- Favor dependency injection.
- Design loosely coupled components.
14. How does Spring Boot use Composition?
Answer
Spring Boot heavily favors composition.
Example
Controller
↓
Service
↓
Repository
↓
Database
A service collaborates with repositories and other services through dependency injection.
Example
@Service
public class OrderService {
private final PaymentService paymentService;
private final NotificationService notificationService;
}
The service has these collaborators rather than inheriting from them.
15. What interview tips should you remember?
Answer
Interviewers commonly ask:
- Composition vs Inheritance
- HAS-A vs IS-A
- Delegation
- Loose coupling
- Tight coupling
- SOLID principles
- Spring Boot architecture
- Enterprise design
- Production scenarios
- Best practices
Remember
- Composition represents HAS-A.
- Inheritance represents IS-A.
- Composition provides loose coupling.
- Inheritance creates tighter coupling.
- Modern Java applications prefer composition.
- Spring Boot relies heavily on dependency injection and composition.
- Use inheritance only when the relationship is naturally IS-A.
- Support every answer with a production example.
Summary
Composition and inheritance are both powerful object-oriented design techniques, but they solve different problems. Inheritance models specialization through IS-A relationships, while composition models collaboration through HAS-A relationships. Modern enterprise Java applications generally favor composition because it promotes flexibility, loose coupling, and maintainability.
Key Takeaways
- Understand HAS-A vs IS-A relationships.
- Learn the differences between composition and inheritance.
- Understand delegation.
- Know when to use inheritance.
- Know when to use composition.
- Learn why modern frameworks favor composition.
- Apply SOLID principles in design.
- Understand Spring Boot architecture.
- Follow enterprise object-oriented design best practices.
- Support interview answers with real production examples.