views:

450

answers:

8

I was discussing multiple inheritance vs. single inheritance with a friend of mine, and discovered that plainly, my conception of Object-Oriented design is completely different than his. I am mostly an Obj-C programmer, so Multiple Inheritance is not something I use daily. He is mostly a C++ programmer under Windows/PSP, so we probably use different concepts on a day-to-day basis.

He actually brought the following subject : What does a new human being inherit from?

My conception of that was that there would be a Human class, and the new being would inherit from that class and get some instance variables (such as his DNA and others) from his two parents.

His conception was that the child would inherit from his two parents, in order to get the methods of his parents.

And now I'm kind of confused, because honestly... Inheriting from objects? Isn't inheritance used to inherit from classes which contain methods common to a certain group of objects? This argument really confused me to no end.

+8  A: 

To take the analogy head-on, the new human gets its traits from a pair of zygotes generated by the 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 wills and contract law?) doesn't mean it has to conceptually match 1:1 with 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 (behaviours, methods...) of either one.

Barry Fandango
I was hoping someone else had written up the "it's just a metaphor, and not a great one at that" answer so I wouldn't have to :)
Jon Skeet
A: 

Well I think the parents are also human. So parents is a property of Human. It has nothing to do with OOP inheritance. It may do so if you are referring to types of human as represented by race such as Caucasian extends Human

ken
+3  A: 

You're both wrong... Both Child and parents are instances of Human Being... . As example, in one possible class structure, HumanBeing (ALL of them) all inherit from "Primate" which Inherits from "Mammal" which Inherits from "Animal"...

Human Being Inherits all behavior of Primate, Like "WalkUpright()", etc.
Primate Inherits all behavior from Mammals, like "Lactate()" "LiveBirth(), etc... Mammal Inherits behavior from Animal...

Each Instance of HumanBeing could have two properties called Father, and Mother, that are each an instance of HumanBeing...

and perhaps another property called Children, that contains a collection of instances of HumanBeing objects...

Charles Bretana
That was my argument: The new child is an instantiated object of class Human which receives some DNA and methods for the classes and learns as it goes. He insisted that it worked better if the child inherited from it's parents, and these parents would have inherited from theirs and so on.
neohaven
I guess you are both "overloading" the word "inherit". Your statement that "new being would 'inherit' from that class and get some instance variables..." implies that you thought he child is a separate class that is inheriting from a parent class.. You can't define a new class for each generation.
Charles Bretana
+6  A: 

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

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

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

    // ...
}

Certainly I can't see:

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

I see the mother and father as being rather involved in the construction of their child, all three are humans.

JeeBee
You're right, the parent child relation is definitively not inheritance. And that is because a class does not inherit from an instance of a class but the class itself.
Loki
The only way to make it the other way, is unless all your objects are singletons, but that's just dumb. (Alice class, alice instance, Bob class, bob instance, AliceAndBobChild class, aliceAndBobChild instance, etc.)
Loki
+3  A: 

Agree with JeeBee, the parents have a construction 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();
    }
}
Barry Fandango
You managed to make me chuckle! Nice! :P
neohaven
A: 

In terms of object oriented behaviour your friend's conception is wrong. A person does not inherit behaviours from their parents in the same way that a class does. A person inherits genes - half from each parent - and these genes combine to cause behaviours. For example even if both parents have a particular eye colour, the child doesn't necessarily have the same eye colour (look up recessive genes if you didn't take high school biology).

In short, don't think about human inheritance when you are thinking about class inheritance. It almost certainly won't be helpful.

DJClayworth
+1  A: 

A human would be a class, and all humans beings belong to the same class.

The child, the father and the mother are only instances of that class.

The mother and father are just factories ( the the human itself composite )

We may inherit from the "missing link" though, it depends of the domain.

But your friend has a point, Achilles for instance inherit from a Nymph and a Human :S hence multiple inheritance proved.

Superman inherit Kriptonian while Spiderman is a human that implements Spider! :P

OscarRyz
A: 

The short answer is that both your and your friend's conception of inheritance have been held by many people and are equally legitimate.

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

Java (particularly recently) has tended to see inheritance as all about managing types and now urges "composition" instead of inheritance for code-reuse, This is partly because inheritance is less flexible in Java than composition so "prefering composition" is good practice. And partly because of the "brittle base-class problem".

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

interstar