|
|
|
|
|
by mikekchar
3094 days ago
|
|
It's hard to ask this question without sounding snarky, but I hope you will understand that I'm seriously curious to hear what you think. > No, my observation has nothing to do with composition vs/ inheritance. > It's about defining what a class IS vs/ what a class HAS. What do you feel is the difference between these two sentences? In other words, why is inheritance vs composition not the same as what a class is vs what a class has? |
|
In practice inheritance is a very special case of composition that is only advantageous in a very narrow set of usecases. Basically almost never.
In fact the number of usecases is so low that I would prefer that modern languages just omit it entirely because the added value is incredibly tiny compared to the cost of confused developers.
The primary difference between an interface and inheritance is that with inheritance you do not have to implement every method and can use the default implementation of the base class.
Example:
Is equivalent to this Inheritance doesn't actually offer any code reuse. All it does is choose the default implementation of the parent class. If you have dozens of polymorphic functions in a class and only want to change 3 it might be convenient. I would question why you need so much polymorphism to warrant a special language feature. C and Rust developers are fine without it. Functional programmers are fine without it. But for some reason in OOP you need it everywhere and are actually worse off because it's overused.Modern language designers, please do not implement inheritance of classes in your new languages!