2 ms·
> for interfaces (as if those were a different thing) Interfaces are a different thing than inheritance, precisely because of what we are talking about. Inher
by jrochkind1 3y ago
> for interfaces (as if those were a different thing)
Interfaces are a different thing than inheritance, precisely because of what we are talking about. Inheritance is about code re-use, and interfaces are about polymorphism. Interfaces are the polymorphism part of "inheritance", while dropping the code re-use part.
It is true that composition does not give you polymorphism, absolutely.
> because the only thing composition solves is code reuse.
Exactly. Inheritance combines polymorphism and code re-use in one form. If you instead don't have inheritance, than interfaces (or something similar by a different name; or "duck-typing" if you don't have static typing and also don't really want any kind of typing at all) give you polymorphism, so what gives you code re-use? Composition.
If you split the code re-use function from the inheritance function, interfaces plus composition is the (or one very common/obvious) solution.
I don't think we are disagreeing, I mean to show you we are all violently agreeing. :)
- marcosdumay 3y agoYeah, inheritance is the union of an interface with a composition with some automatic linking between them. Thus I agree they are not literally the same thing. I do see some value on the "automatic linking" part, but deciding you don't want the complex mixed concept on your idiom isn't any radical idea that will change your life (neither for the better nor for the worse).
- rramadass 3y ago> Inheritance combines polymorphism and code re-use in one form. Not necessarily; see "Interface vs. Implementation (eg. private derivation in C++) inheritance".