4 ms·
The controversy over inheritance has to do with implementation inheritance, where any method that's defined on a base class can depend on methods that may have
by 0815test 7y ago
The controversy over inheritance has to do with implementation inheritance, where any method that's defined on a base class can depend on methods that may have been overridden in some derived class, or rely on some non-codified invariants that a derived class might break: this is known as the "fragile base class" problem. By using composition over inheritance, this is not something that can happen by default, because there is no provision for a method call on some "base" method to dispatch on the object's "actual" type, or more broadly to rely on anything but what's defined in the base object itself. You can recover that dispatch-like behavior ("open recursion"), but it's something you have to code yourself and opt into explicitly. This is good for rigor and auditability, as well as just being all-around simpler and easier to understand. Implementation inheritance is too general of a feature, and just not a good abstraction.
- WoodenChair 7y agoI'm very aware of the fragile base class problem. But it's a non-problem for many of the small class hierarchies that protocol-oriented programming examples are often used as examples of replacing. Surely, there would be no fragile base class problem here with a total of 1 base class and 3 subclasses. If you're doing something like the Swift standard library, protocol-oriented programming is a huge win. If you're doing a small class hierarchy as you would be to do this example, I don't see the big win.