The opposition I've read to use composition over inheritance is two fold. One is inheritance breaks encapsulation, because child classes can access protected members of the parent class, and two is maintainability.
Another thing I've read concerning inheritance is the base/parent class should only be an Interface thus putting ALL implementation code in child classes. This supposedly increases maintainability with inheritance, but I don't really understand the reasoning.
Here are my questions and thoughts for discussion. I bring them here solely because this is the only coding community I belong to. I realize that a lot of you are either beginners or still learning like me, but perhaps something like this will help in the long run.
- Composition:
- What is it really?
- Is it just taking an object and making it a member of another object?
- If so, how is this comparable to inheritance?
Inheritance: - How is inheritance breaking encapsulation when that kind of seems like the whole point of inheritance by definition, to 'inherit' from? Child classes can't access the parent's private members directly, so there's still that encapsulation.
- Interfaces are base classes that have no implementations and no data members, there is no code reuse. I understand chili specifically pointed out NOT using inheritance specifically for code reuse and from what I have found forcing your code to do so always causes headaches.