🧠Inheritance vs Composition in Java — When to Use Which?
💡 Understanding the Difference
Both inheritance and composition are ways to reuse code and model relationships between classes in object-oriented programming — but they are used in different scenarios.
🧩 When to Use Inheritance
-
Use inheritance when there is a clear “IS-A” relationship between two classes.
Example:-
An
Employeeis aPerson. -
A
Caris aVehicle.
-
-
Inheritance is preferred when a subclass needs to expose or override all behaviors of its parent class.
✅ Key idea: Inheritance allows one class to extend another and automatically gain access to its properties and methods.
🧩 When to Use Composition
-
Use composition when there is a “HAS-A” relationship.
Example:-
A
Carhas aEngine. -
A
CompanyhasEmployees.
-
-
Composition is preferred when a class needs some behaviors or data from another class but does not need to inherit everything.
✅ Key idea: Composition uses an object reference to another class instead of extending it, allowing greater flexibility and less coupling.
🧠Example 1 — Using Inheritance
🧩 Output
Here, the Employee class inherits from Person (an IS-A relationship).
So, Employee automatically has access to all methods of Person, including getPersonCountry().
⚠️ Limitation of Inheritance
If you try to change the return type of getAge() in the Employee class from int to String, the compiler will throw an error.
That’s because when you use inheritance, method signatures must match exactly, and return types must be compatible with the parent method’s return type.
🧩 Example 2 — Using Composition
In cases where you only need some properties or different implementations, inheritance may not be suitable.
Here’s how composition helps:
🧩 Output
Here, Employee1 has-a Person1 object inside it and uses it to call getPersonCountry().
This is composition — Employee1 is not a Person1, but it uses one.
⚙️ Summary — When to Use Which
| Scenario | Preferred Approach | Relationship Type |
|---|---|---|
| A subclass should inherit all properties and behaviors from a parent class | Inheritance | IS-A |
| A class should only use certain behaviors or data from another class | Composition | HAS-A |
| You want to change or customize method signatures (e.g., return type) | Composition | HAS-A |
You need polymorphic behavior (Person p = new Employee()) |
No comments:
Post a Comment