3 ms·
In my experience the pitfalls of inheritance are something you have to really see for yourself in practice, so it generally takes even very good developers year
by akeefer 18y ago
In my experience the pitfalls of inheritance are something you have to really see for yourself in practice, so it generally takes even very good developers years to really develop the intuition around when to use inheritance and when to use composition. Inheritance is a blunt tool that's easy to use, while composition is trickier and requires more skill and foresight to use correctly. There are also more different styles of composition that accomplish slightly different things, so you have to know which one will work best for your problem. My personal experience is also that many people just don't understand compositional models as readily either.
One of the big problems, though, is that inheritance is much easier in most programming languages than composition is. Java, for example, has absolutely no support for composition or delegation, whereas inheritance requires a single keyword. C++ has multiple inheritance, which you can kind of re-purpose for the sake of composition, but it's still not very elegant and tends to just exacerbate the misuse of inheritance instead. Ruby has a pretty elegant mixin model, and you can pretty much do whatever you want in Javascript if you're willing to roll the solution yourself (I confess that my Python knowledge is more limited so I have no idea if there's support there), but the most mainstream OO languages require you to really fight them in order avoid using inheritance inappropriately.