What are the tradeoffs of using composition over inheritance?
Last updated
Composition ("has-a", holding member objects) gives looser coupling and lets components be swapped at runtime. It also avoids fragile base classes and deep hierarchies.
Costs: boilerplate forwarding methods, and no automatic subtype polymorphism unless you add an explicit interface.
Rule of thumb: use inheritance only for genuine "is-a" with substitutability, otherwise prefer composition.
- https://stackoverflow.com/questions/2658288/multiple-inheritance-debates-ii-according-to-stroustrup
- https://www.interviewbit.com/cpp-interview-questions/
- https://gist.github.com/garrettmac/71cf01eca4e09f34e5c503b2bbf51f9f
- https://dev.to/master-rj/oop-in-c-explained-is-a-vs-has-a-relationship-inheritance-vs-composition-guide-g5o
- https://dev.to/pandax185/object-oriented-programming-in-go-abalanced-comparison-with-traditional-oop-36l8
- https://github.com/Devinterview-io/oop-interview-questions