• MiddleKnight
    link
    fedilink
    English
    arrow-up
    10
    ·
    edit-2
    1 year ago

    There are many reasons one might use inheritance. The one which is often first introduced by OOP proponents is creating a “is-a” relationships in some domain model where the goal is to somehow model “real life” with classes and objects.

    This always ends badly in my experience. Not worth doing. Just consider the “rectangle square controversy” https://henrietteharmse.com/2015/04/18/the-rectanglesquare-controversy/. Inheritance as a domain modelling tool doesn’t really mean anything outside of textbooks talking about animals and vehicles.

    One should understand how computers and programming work and model data in a way which makes sense for the concrete scenario. Look in any algorithms and data structures book. Nothing is ever explained and presented through object models. No UML diagrams needed.

    But inheritance also bring actual concrete functionality. In particular dynamic single dispatch. If you need dynamic single dispatch, inheritance is good. That being said, I think that parametric polymorphism (“generics” in OOP terminology) is a better solution than subtype polymorphism in almost all cases. Although languages build around OOP and inheritance sometimes make it too inconvenient to use.