0

Recently I was trying to solve a design problem in Java for a scenario, wherein

A Child inherits some properties from Father and some Mother.

For example: "Child" class has to inherit few properties/behaviors such as faceCut, snoringWhileAsleep and height from "Father" class and few other properties hairColor from "Mother" class. Not all properties of Father and Mother are inherited by Child.

How Java design patter could resolve this problem?

M Arch
  • 103
  • 10
  • multiple inheritance? It was a design decision to not to support it. There solutions (generally better) : http://stackoverflow.com/questions/1262447/multiple-inheritance-in-java – Jayan Jan 24 '14 at 06:28
  • Interface. `IFaceCutable`, `ISnoringWhileAsleepable`, `IHairColorable`. Make the Child class constructor accept `Father` and `Mother` at the same time. – Fendy Jan 24 '14 at 06:29
  • what you require can be achieved using interface but the way you explained (Using Father and Mother Classes) can't be achieved in Java – Malav Jan 24 '14 at 06:34
  • Genetics doesn't work this way. The inheritance of genetic traits from your mother or father is essentially random. And in some cases, the trait will come from both parents. The only traits that always (i.e. for all children) come from exclusively from the father or mother are those that involve the Y chromosome; i.e. they only affect male children. In UML terms, this "design" is more an Object Diagram than a Class Diagram. So while the problem may be useful as an exercise, it is not related to reality. – Stephen C Mar 15 '20 at 03:38
  • P.S. I think I am saying what Malav said ... – Stephen C Mar 15 '20 at 03:44

2 Answers2

0

// Alternative one

public class Child implements Mother, Father {

    @Override
    public void hairColor() {
        // TODO Auto-generated method stub

    }

    @Override
    public void height() {
        // TODO Auto-generated method stub

    }

}

interface Mother {
    void hairColor();
}

interface Father {
    void height();
}

// Better Alternative

abstract class Parents implements Mother, Father {
    @Override
    public void hairColor() {
        // TODO Auto-generated method stub

    }

    @Override
    public void height() {
        // TODO Auto-generated method stub

    }
}

class Child extends Parents{

}
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
0

You are basically introducing the problem of multiple-inheritance in java which is not possible by design.
I see two solutions were already suggested using interfaces.
Another solution is using delegation. meaning, the father and mother are fields in the child object and every method you would like to "inherit" from them is delegated to them, i.e is activated from these fields.

public class Human {

    String hairColor;
    boolean snoreWhileAsleep;

    public String returnHairColor() {
        return hairColor;
    }

    public boolean isSnoring() {
        return snoreWhileAsleep;
    }
}
public class Child extends Human {

    Human father;
    Human mother;

    public String returnHairColor() {
        return mother.returnHairColor();
    }

    public boolean isSnoring() {
        return father.isSnoring();
    }
}
BeUndead
  • 3,463
  • 2
  • 17
  • 21
Tom
  • 1,203
  • 4
  • 21
  • 36