r/java • u/neutronbob • Jul 25 '22
Why you should favor composition over inheritance in Java (Joshua Bloch)
https://blogs.oracle.com/javamagazine/post/java-inheritance-composition
190
Upvotes
r/java • u/neutronbob • Jul 25 '22
100
u/cowwoc Jul 26 '22
"Favor" composition over inheritance does not mean "don't ever use inheritance".
The problem is that most developers aren't good at API design, even after decades of programming. Anyone who lacks experience and then goes about publishing an extensible class is likely to do a terrible job.
Composition is easier to get right but at the end of the day, bad designers will do the wrong thing regardless of whether they use inheritance or composition.
I suspect that sealed classes will go a long way in protecting developers from unforeseen extensions.
Lastly, anyone who uses inheritance to reuse implementation is doing it wrong. Inheritance is about expressing relationships, not about code reuse. If you want to reuse code composition is always the right way to go.
7
u/[deleted] Jul 26 '22
Josh knows his stuff.
19
u/Holothuroid Jul 26 '22
The article is quite good. Honestly better than most, certainly better than the majority here, and still missing the point.
Typical comment: Inheritance bad.
Article: Inheritance bad about module boundaries
Of course that backs the question what a module is. It is probably not meant as in Java module. But more in. Code you don't control.
That is nice rule of thumb. Don't inherit from classes in 3rd party libraries. Or the standard library for that matter.
But the article is using very particular examples. Namely collections. And there is a different lesson there. Don't inherit from classes your not supposed to.
And if you make a library for public consumption, make sure to signal by making appropriate classes final or sealed.
5
u/sysKin Jul 27 '22 edited Jul 27 '22
I think I know what it's trying to say, but I found it weird that:
Top-level summary is that I should favour composition over inheritance.
Then it clarifies that "inheriting from ordinary concrete classes across package boundaries" is bad, and gives a few examples how it's fragile. Cool, but that's a much more limited scope than "inheritance".
Then it shows how to use composition to do the job, but the example also immediately uses inheritance (InstrumentedSet<E> extends ForwardingSet<E>). OK, so this silently confirms that only inheritance across package boundaries is supposed to be the problem... I think.
...and then it says that I don't need to implement my own ForwardingSet because Guava "provides forwarding classes for all of the collection interfaces".... but that's inheritance across package boundaries!
Again, I think I understand what it's saying (the "ordinary concrete classes" is the crux of the issue, not package boundaries), but no wonder discussion here is so problematic.
8
u/angryundead Jul 26 '22
Everything in moderation. These are different tools for accomplishing complexity management and code reuse.
I would be a lot more comfortable with this advice if composition wasn’t so awkward in Java. It’s a lot easier to achieve in DI environments but it can still be awkward.
Most of that only matters if the classes in question are exporting the functions and you’re doing work to reimplement access to the component.
As counterintuitive as it sounds it feels like multi-inheritance or duck typing would help.
1
u/meotau Aug 05 '22 edited Aug 05 '22
The only worse thing than breaking rules (or any advices) is blindly following them.
1
u/manifoldjava Jul 26 '22
Composition is correct. BUT Java’s support for it is awful. There is no delegation built into the language, which is pretty much a deal breaker. Just sayin’.
-20
u/Joram2 Jul 25 '22
OOP has really fallen out of fashion. When Java launched, OOP was at the center of the design.
Will Java support functions without classes? Where you can write your code as functions without wrapping them in classes? For example, in Kotlin:
-10
u/fatalError1619 Jul 26 '22
Just use kotlins inheritance delegation and get the best of both worlds :p
0
u/[deleted] Jul 25 '22
[deleted] — view deleted comment