3 ms·
> 3. Often you want to use the default protocol implementation with some changes. Instead of re-implementing the rest of the protocol, it's highly convenient to
by tome 7y ago
> 3. Often you want to use the default protocol implementation with some changes. Instead of re-implementing the rest of the protocol, it's highly convenient to be able to specify only the differences.
> #3 is the core reason why inheritance was developed.
No, absolutely not. As sibling comments say, composition is sufficient for that. I can only see exactly one reason why inheritance is necessary: to allow superclasses to call methods defined in subclasses, also known as late binding.
- colllectorof 7y agoFirstly, late binding is orthogonal to inheritance. It simply means that methods are looked up based on runtime information, rather than determined at compile time. You can have late binding in a language with no inheritance or classes at all. Secondly, OO composition does not solve the problem I described in #3. If object X has 9 methods I want to reuse in object Y, making X a component of Y still requires me to re-implement those 9 methods in Y, even if they are just pass-throughs.
- couchand 7y ago> If object X has 9 methods I want to reuse in object Y, making X a component of Y still requires me to re-implement those 9 methods in Y, even if they are just pass-throughs. That is also not universal. In Ruby, for instance, one can write: class Y def method_missing(m, *args, &block) @x.send(m, *args, &block) end end
- smrq 7y agoI don't think you have to squint very hard to see that as an implementation of inheritance.
- tome 7y agoActually, I shouldn't have made claims about "the core reason why inheritance was developed" because I've no idea why it was developed. On the other hand, I believe I know why it is necessary (as opposed to convenient). In many languages there is functionality it provides that is otherwise unavailable. In Java, or C++, or Python, how can we allow superclass methods to call methods on a subclass (a form of late binding) without inheritance? I don't think it's possible. So unless I am much mistaken (and please correct me if I am wrong), inheritance is convenient for reducing boilerplate but necessary for late binding (in many languages). Have I made a mistake in my analysis somewhere?
- kd5bjo 7y agoThe “superclass” can take a config object with a well-defined interface in its constructor, and call methods on this config object when it might need to do something differently.
- BubRoss 7y agoHe never said nothing else could accomplish the same thing, only that it was the reason it was invented.