I have heard this favor composition over inheritance
again and again in design patterns. some of the reasons cited for this are
1)Inheritance is strongly coupled where as composition is loosely coupled
2) Inheritance is compile time determined where as composition is run-time
3)Inheritance breaks encapsulation where as composition does not
4) anything else I am not aware of
It would be great for beginners like to me follow these by illustrations as how inheritance and composition differs with respect to above points. I have read various SO links that talks about them, but walk through examples on these key points would be great for Java beginners.
I think it is very crucial to understand the difference clearly than just memorizing the points.
Nice question and big for a beginner, I think I should first remind the reader what inheritance and composition are, and then go on to explain what exactly
Favor Composition over Inheritance
means.Pros and Cons of Inheritance :
Advantages:
Disadvantages:
details of its super-class.
White-box
reuse, since internal details of super-classes are often visible to subclasses.About the question:
Inheritance will bring you tight coupling, simply one change to base class can break many child classes.
But when to use and how detect we need inheritance or composition?
Use inheritance only when all of the following criteria are satisfied (Coad's Rule):
is a special kind of
and notis a role played by a
.When compiled, your base class codes will be added to every child class.
Yes. Now you see the disadvantage of inheritance.
The bottom line is:
Make sure inheritance models the is-a relationship My main guiding philosophy is that inheritance should be used only when a subclass is-a superclass. In the example above, an
Apple
likely is-aFruit
, so I would be inclined to use inheritance.An important question to ask yourself when you think you have an is-a relationship is whether that is-a relationship will be constant throughout the lifetime of the application and, with luck, the lifecycle of the code. For example, you might think that an
Employee
is-aPerson
, when reallyEmployee
represents a role that aPerson
plays part of the time. What if the person becomes unemployed? What if the person is both anEmployee
and aSupervisor
? Such impermanent is-a relationships should usually be modelled with composition.Don't use inheritance just to get code reuse If all you really want is to reuse code and there is no is-a relationship in sight, use composition.
Don't use inheritance just to get at polymorphism If all you really want is polymorphism, but there is no natural is-a relationship, use composition with interfaces.
Favor composition Over Inheritance :)
I took it straight from the javaworld.
Inheritance is expressed in code using extends for concrete classes. Once you write it, you can't change it without rewriting the classes. I can only change by modifying code.
I can only change what
Bar
does by modifying one or both classes.Composition is usually interface-based, which means that you specify what is done, not how. You can change the how without affecting clients simply by changing the implementation of the interface.
I can change the behavior of
Bar
in this case simply by passing a different implementation of theFoo
interface.It's one of Bob Martin's SOLID principles: The Open/Closed Principle.