+ 1

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?

23rd Oct 2019, 5:23 AM
viki
viki - avatar
2 Antworten
+ 4
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.
23rd Oct 2019, 8:09 AM
HonFu
HonFu - avatar
+ 2
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!
23rd Oct 2019, 9:26 AM
Schindlabua
Schindlabua - avatar