The concept of inheritance in object-oriented languages

I was discussing multiple inheritance versus single inheritance with a friend of mine and found myself clear that my concept of object oriented design is completely different from his. I'm basically an Obj-C programmer, so Multiple Inheritance is not something I use on a daily basis. He's basically a Windows / PSP C ++ programmer, so we probably use different concepts on a daily basis.

In fact, he gave the following topic: What does the new person inherit?

My concept was that there would be a class of Human, and the new creature would inherit from this class and receive some instance variables (like his DNA and others) from his two parents.

His concept was that a child would inherit from his two parents in order to receive his parents' methods.

And now I'm a little confused because honestly ... Inherit from objects? Doesn't inheritance inherit from classes that contain methods common to a specific group of objects? This argument really confused me to the end.

+1


source to share


8 answers


The parents are also humans who are part of a family of creatures called mammals. Your thoughts seem to me the most logical.

public class Human extends Mammal implements HunterGatherer, Speech, CognitiveThought {

    public Human(Human mother, Human father) {
        super(mother, father);
        // ...
    }

    // ...
}

      

Of course I don't see:



public class Human extends Mother, Father { ... }

      

I see that the mother and father are involved in the construction of their child, all three are people.

+7


source


To accept the analogy in their heads, a new person gets their traits from a pair of zygotes spawned by parents, which are only loosely based on the parents' own DNA.

Realistically, I think this is just a bad analogy. Just because "inheritance" is a borrowed term from genetics (or will and contract law?) Does not mean that it has to be conceptually 1: 1 in software development, and in fact they are only superficially similar concepts.



For example, my father is a lawyer and my mother is a school teacher. I was not born with the skills (behavior, methods ...) of one of them.

+8


source


You're both wrong ... Both the child and the parents are Human people .... For example, in one possible class structure, HumanBeing (ALL of them) inherits from "Primate", which Inherits from "Mammal", which Inherits from "Animal "...

Human Being Inherits all Primate behavior, like "WalkUpright ()" etc.
Primate Inherits all behavior from mammals such as "Lactate ()" "LiveBirth (), etc ... Mammal Inherits behavior from Animal ...

Each HumanBeing instance can have two properties called "Father" and "Mother", each of which is an instance of HumanBeing ...

and possibly another property called Children, which contains a collection of HumanBeing object instances ...

+3


source


Agree with JeeBee, parents have a building role!

public Class HumanFactory(Human mother, Human father)
{
    public Human NewHuman()
    {
        while(!mother.IsPregnant)  // may loop infinitely in the infertile case
            father.Mate(mother)
        while(mother.IsPregnant)
            System.Threading.Sleep(1000); // may take some months, get comfortable
        return mother.DeliverBaby();
    }
}

      

+3


source


Man will be a class and all people belong to the same class.

Child, father and mother are just instances of this class.

Mother and father are just factories (the man himself is a composite)

We can inherit from "missing link" , although this is domain dependent.

But your friend has a point, Achilles, for example, inherits from Nymph and Man: S, therefore, multiple inheritance is proven.

Superman inherits Kryptonian, while Spider-Man is the man who realizes Spider !: P

+1


source


Well, I think parents are people too. So the parents are the property of Man. It has nothing to do with OOP inheritance. He can do this if you mean the types of people represented by the race, such as the Caucasian language extending the Human

0


source


In terms of object oriented behavior, your concept of friends is wrong. A person does not inherit behavior from their parents in the same way that a class does. A person inherits genes - half from each parent - and those genes combine to induce behavior. For example, even if both parents have a particular eye color, the child does not necessarily have the same eye color (look for recessive genes if you haven't taken high school biology).

In short, don't think about human inheritance when you think about class inheritance. It almost certainly won't help.

0


source


The short answer is that both yours and your familiar concept of inheritance have been held by many people and are equally legitimate.

Your concept of friends is closer to "object inheritance" in "prototype-based languages" (eg Self and Javascript). Yours is more in line with "class inheritance" which is typical of Smalltalk, Java and, yes, C ++.

Java (especially recently) tends to see inheritance as all about type management and now strongly encourages "composition" instead of inheritance for code reuse. This is in part because inheritance is less flexible in Java than composition, so "preferred composition" is good practice. And partly because of the "fragile base class problem".

But if your friend is more used to creating "mixins", he may very well see inheritance more as a kind of library. This may not be best practice in Java or C ++, but this is the way inheritance has often been understood.

0


source







All Articles