Is Inheritance really needed?

后端 未结 22 2042
孤街浪徒
孤街浪徒 2020-12-14 00:20

I must confess I\'m somewhat of an OOP skeptic. Bad pedagogical and laboral experiences with object orientation didn\'t help. So I converted into a fervent believer in Visua

相关标签:
22条回答
  • 2020-12-14 01:16

    Inheritance lets me push off a whole bunch of bookkeeping onto the compiler because it gives me polymorphic behavior for object hierarchies that I would otherwise have to create and maintain myself. Regardless of how good a silver bullet OOP is, there will always be instances where you want to employ a certain type of behavior because it just makes sense to do. And ultimately, that's the point of OOP: it makes a certain class of problems much easier to solve.

    0 讨论(0)
  • 2020-12-14 01:17

    Inheritance defines an "Is-A" relationship.

    class Point( object ):
        # some set of features: attributes, methods, etc.
    
    class PointWithMass( Point ):
        # An additional feature: mass.
    

    Above, I've used inheritance to formally declare that PointWithMass is a Point.

    There are several ways to handle object P1 being a PointWithMass as well as Point. Here are two.

    1. Have a reference from PointWithMass object p1 to some Point object p1-friend. The p1-friend has the Point attributes. When p1 needs to engage in Point-like behavior, it needs to delegate the work to its friend.

    2. Rely on language inheritance to assure that all features of Point are also applicable to my PointWithMass object, p1. When p1 needs to engage in Point-like behavior, it already is a Point object and can just do what needs to be done.

    I'd rather not manage the extra objects floating around to assure that all superclass features are part of a subclass object. I'd rather have inheritance to be sure that each subclass is an instance of it's own class, plus is an instance of all superclasses, too.

    Edit.

    For statically-typed languages, there's a bonus. When I rely on the language to handle this, a PointWithMass can be used anywhere a Point was expected.

    For really obscure abuse of inheritance, read about C++'s strange "composition through private inheritance" quagmire. See Any sensible examples of creating inheritance without creating subtyping relations? for some further discussion on this. It conflates inheritance and composition; it doesn't seem to add clarity or precision to the resulting code; it only applies to C++.

    0 讨论(0)
  • 2020-12-14 01:21

    No.

    for me, OOP is mostly about encapsulation of state and behavior and polymorphism.

    and that is. but if you want static type checking, you'll need some way to group different types, so the compiler can check while still allowing you to use new types in place of another, related type. creating a hierarchy of types lets you use the same concept (classes) for types and for groups of types, so it's the most widely used form.

    but there are other ways, i think the most general would be duck typing, and closely related, prototype-based OOP (which isn't inheritance in fact, but it's usually called prototype-based inheritance).

    0 讨论(0)
  • 2020-12-14 01:23

    There are a lot of features in a programming language that are not really needed. But they are there for a variety of reasons that all basically boil down to reusability and maintainability.

    All a business cares about is producing (quality of course) cheaply and quickly.

    As a developer you help do this is by becoming more efficient and productive. So you need to make sure the code you write is easily reusable and maintainable.

    And, among other things, this is what inheritance gives you - the ability to reuse without reinventing the wheel, as well as the ability to easily maintain your base object without having to perform maintenance on all similar objects.

    0 讨论(0)
提交回复
热议问题