Is it true that composition is better than inheritance r is it just a opinion based debate?

If they r not tell us the reason Abt the advantages and disadvantages they have on each other.Had read some books claiming that inheritance makes the code to be tightly coupled and makes it difficult to make changes.Is it so?

10/23/2019 5:23:09 AM


2 Answers

New Answer


I have recently read this, the author touches the problem a bit in his article. Probably he's right with his statement that all of these things are 'tools' - you use them when they fit the task, not because you agree with a dogma.


Inheritance is a tighter coupling than composition, yes. Derived classes can access public and protected members, a class using an instance of another class only public ones. Also derived classes implementing virtual functions form a pretty tight coupling as the base class can now call the derived class. For those reasons I try to avoid inheritance, but some classes will just be tightly coupled, whatever you do, and sometimes inheritance is the most elegant choice!