| Some months ago, I stumbled into an OOP inheritance rabbithole which got me thinking the same: - Isn't the (biological) concept of inheritance built at it's core around the idea of "generations"? How does that make any sense in OOP? - Why do Java beginner courses still teach Inheritance like it was 1995 with all the bells and whistles and dogs and cats and mammals? - Why do they teach Inheritance and later introduce "favor composition to inheritance"? Doesn't this just confuse everyone? - Why does Inheritance have first-class syntax support in an OOP language like Java ("extends") whereas Composition usually needs to be "engineered" using some more complex patterns, e.g. DI? Most of it is probably for historical reasons, i guess. |
I think the main reason is that DI is seen as an orthogonal concept by language designers, so you'd need new two first-class features in a language.
If you do composition without injection (e.g.: by having new ChildObject() in the constructor), you don't really require that many more lines of code compared to inheritance.
Of course it's much less flexible and less testable than composition + injection, but not that inflexible when compared to inheritance. And first-class support for only that would make the feature a bit useless without DI...