Where does this concept of "favor composition over inheritance" come from?

Posted by Mason Wheeler on Programmers See other posts from Programmers or by Mason Wheeler
Published on 2011-04-04T22:54:14Z Indexed on 2012/11/26 23:23 UTC
Read the original article Hit count: 426

In the last few months, the mantra "favor composition over inheritance" seems to have sprung up out of nowhere and become almost some sort of meme within the programming community. And every time I see it, I'm a little bit mystified. It's like someone said "favor drills over hammers." In my experience, composition and inheritance are two different tools with different use cases, and treating them as if they were interchangeable and one was inherently superior to the other makes no sense.

Also, I never see a real explanation for why inheritance is bad and composition is good, which just makes me more suspicious. Is it supposed to just be accepted on faith? Liskov substitution and polymorphism have well-known, clear-cut benefits, and IMO comprise the entire point of using object-oriented programming, and no one ever explains why they should be discarded in favor of composition.

Does anyone know where this concept comes from, and what the rationale behind it is?

© Programmers or respective owner

Related posts about object-oriented

Related posts about inheritance