4 ms·
I wouldn't even call "polymorphism" an "aspect of inheritance" -- you can have polymorphism _without_ inheritance (as a code re-use form), and polymorphism is a
by jrochkind1 3y ago
I wouldn't even call "polymorphism" an "aspect of inheritance" -- you can have polymorphism _without_ inheritance (as a code re-use form), and polymorphism is actually more fundamental and importance than inheritance.
I feel like this "hot take" is actually a pretty common take.
You can have polymorphism without inheritance; you can't really have inheritance without polymorphism. To the extent that inheritance necessarily "includes" polymorphism, so polymorphism is in that sense an aspect -- yeah, it's more important and fundamental.
And I think this is actually a pretty common take. All the takes saying inheritance itself is not fundamental to OO (and even sometimes saying OO is better without inheritance), are variations of saying this.
- ozim 3y agoWith nowadays approach where usually people favor composition over inheritance all is true that OO is better without inheritance.
- marcosdumay 3y agoExcept that composition doesn't give you polymorphism. I always though that dichotomy missed the point. You can give-up inheritance for generics, for interfaces (as if those were a different thing), for type classes, or for type ducking; but you can't give-up inheritance for composition because the only thing composition solves is code reuse.
- 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".
- jsunderland323 3y agoWRT inheritance, composition solves the diamond problem. It’s really about decoupling interfaces for portability more than code reuse. I think the big abstraction problem inheritance solves is overrides. I think there’s a lot of FP cargo culting that overlooks the benefits of the ability to override (when used wisely). But aside from that I’m not sure there’s any reason to think inheritance is any more or less polymorphic than composition for a language with sufficient reflection/introspection support. If you lack type introspection it’s a different story. Maybe I’m missing your point though.
- whstl 3y agoYou can still have polymorphism without implementation inheritance, which is the problematic aspect of inheritance. Interfaces are fine.
- ozim 3y agoIt is not a dichotomy - it is not all or nothing and it is not mutually exclusive - it is not "use composition always and never ever use inheritance". Favor composition is just that your solution will most likely be better with it. There are still cases where you want inheritance but also most likely you don't want to create class hierarchy with 5 levels and everything inheriting form everything else but maybe have have a base class. So even in single application or microservice you might still have some inheritance and most likely more composition. Besides composition uses interfaces heavily because you want to use DI with constructor injection and then you can switch implementation even at run time.
- Fire-Dragon-DoL 3y agoInterface implementation does though, that was the whole point. The problem with inheritance is the code reuse mechanism
- soulbadguy 3y ago> you can't really have inheritance without polymorphism. "private inheritance" which is really composition is inheritance without polymorphism
- gorkempacaci 3y agoYou can have inheritance without polymorphism if you don’t override any of the inherited behavior?
- spacechild1 3y ago> you can't really have inheritance without polymorphism. That's not true. In C++ you get inheritance without (runtime) polymorphism if all methods are non-virtual. It's very common actually.
- deleted 3y ago[deleted]