3 ms·
I think of inheritance as just being a workaday implementation detail to facilitate code reuse in some scenarios. Polymorphism ("many forms") is more related to
by frou_dh 3y ago
I think of inheritance as just being a workaday implementation detail to facilitate code reuse in some scenarios. Polymorphism ("many forms") is more related to the concept of interfaces, but as you touch on has been equated with inheritance in a lot of peoples' minds.
- _a_a_a_ 3y agoInheritance should be used for interfaces, not code reuse (unless that comes free with it).
- nradov 3y agoThen how do you avoid duplicate code without using inheritance? Other solutions like placing shared functions in "helper" classes are far worse from a complexity and maintainability standpoint.
- javcasas 3y ago"helper" classes are a symptom of an object-obsessed programming language, a language where functions can't live in a module, they must always exist in a class. Especially if it doesn't make sense for a class to hold them. You avoid duplicated code by making generic functions, putting them in modules, and making your objects accept them as parameters.
- nradov 3y agoWell I've done that before and the results were no better than using inheritance. What's the point?
- _a_a_a_ 3y agoI guess it's like this – implement interfaces, and common code comes with it when you inherit (then override to modify behaviour). A common interface really should have common code so this is appropriate. What is wrong is to inherit from an object with a different interface just to get your hands on the code - that might seem obvious but when OOP started to be used in industry, that was acceptable or even recommended practice. That malpractice was formalised away by Barbara Liskov with the Liskov principle. I'm pretty certain you're doing the right thing already so it was a clarification rather than a criticism.
- magicalhippo 3y agoIn Delphi, this is my preferred way. I use interfaces[1]for the public-facing stuff, but I might use inheritance to implement the interfaces for code reuse and ease. I of course also use delegation a lot, but for some things I feel inheritance is a better fit. Regardless, the inheritance remains an implementation detail. [1]: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Interface_References_(Delphi) https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Inte...