I was going through the difference between inheritance and composition. Everyone has praised composition over inheritance, so questions is how do we make choice ? Also if we didn't use inheritance at all then what are we going to miss as per OOP principal ?
-
Are you suggesting that composition is always right ? – Datta Diware Apr 05 '20 at 20:39
-
1No, but exercise extreme care to be sure it really is a is-a relationship. It’s very inflexible. Lots of design patterns are there as better alternatives to inheritance. – Nathan Hughes Apr 05 '20 at 20:44
4 Answers
It really comes down to how your data is structured. Inheritance is used for "is a" relationships while composition is used for "has a" relationships.
For example, if you were look at cars.
Inheritance: you would have 'Toyota', 'Honda', 'BMW' under 'Car' because they are types of cars.
Composition: you would have 'Engine', 'Wheels', 'Door' under 'Car' because they are parts that make up the car.
'Toyota', 'Honda', 'BMW' aren't parts of a car same way 'Engine', 'Wheels', 'Door' aren't types of cars.

- 21
- 4
-
Can you suggest between these ? public class Person {} public class Employee extends Person {} public class Customer extends Person {} Or public class Person {} public class Employee { private Person person; } public class Customer { private Person person; } – Datta Diware Apr 05 '20 at 20:56
-
1In that case, neither composition nor inheritance is appropriate: a person can be an employee (perhaps of many companies) and a customer (perhaps of many businesses); they may start or stop being an employee/customer. What you've got there is an [*association*](https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition) with a company/business. – Andy Turner Apr 05 '20 at 21:09
This is a big complicated topic, perhaps too much to be described in a few SO posts. Inheritance can be tricky, People have done Ph.D dissertations on this. Most of the time when I write new code I keep inheritance to a minimum.
When your Dog class inherits Mammal all the state/behavior of Mammal must still apply. (the dog is a mammal). Is a platypus a mammal? Biology-yes, but it may not apply to your needs. Especially when you have mutable objects you run into messy situations such as https://en.wikipedia.org/wiki/Circle%E2%80%93ellipse_problem#Description
Also in real life there's often goofy couplings between parent and child classes.

- 5,168
- 1
- 24
- 37
You cannot say that one should be preferred over another. It depends how the data is structured and what representation is required for a particular data model. There is a simple way to remember what to use:
Inheritance = is
- The fact an object inherits from another means the object actually is what it inherits from, hierarchically. Let's take an example from the simplified taxonomy in biology. Working with the kingdom Animalia, we can say that Opossum is a Mammal, which is Chordate, which is animal. Therefor here we have a chain of the inheritance:
Opossum extends Mammal
,Mammal extends Chordate
,Chordate extends Animal
. Each of the group has various characteristics that all the subtypes inherit.
Composition = has
The composition is has. However, here this relation should be rather understood as is composed from or contains. If we stay in the world of biology, we can say an Opossum has a head, tail, body and 4 legs, therefore there is a composition relationship between these objects. This is in Java represented as instance fields:
class Opossum { Head head; Body body; Leg[] legs = new Legs[4]; }

- 40,893
- 16
- 117
- 183
You can google thousands articles about it. So if you want to understand more deeply, google and learn, we dont have to reinvent wheel here.
If you want short explanation what composition over inheritance
means, it basically says - do not overengineer your solution with some super complex inheritance. If you develop something and you see some clear advantages of inheritance and you cant achieve the same with composition, you are free to use inheritance. If you dont see it or you can achieve the same with composition, use composition.

- 22,239
- 9
- 44
- 87