3 ms·
Class composition is about reuse. Class inheritance isn't.
by oweiler 3y ago
Class composition is about reuse. Class inheritance isn't.
- raddan 3y agoThat may be the case in practice but it is not literally true, since semantically, inheritance is effectively a form of copy-and-paste in the compiler. It is literally about reusing components written elsewhere. I would argue that both are forms of code reuse, with the latter also having consequences for type checking.
- rewmie 3y ago> That may be the case in practice but it is not literally true, since semantically, inheritance is effectively a form of copy-and-paste in the compiler. No, not really. The whole point of inheritance is declaring new types by implementing specifying interfaces in a way that specify type hierarchies. If your goal was code reuse and "copy-and-paste" then nothing would beat composition. If you decide instead to go with inheritance, you have far more requirements to meet that go way beyond code reuse.