user17695302
user17695302

Reputation:

Implement bidirectional full relation between two objects

I have 2 classes Mother and Child

Class Mother :

public class Mother {

    private int id;
    private String name;
    private int age;

    public Mother(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

Class NewBorn :

  public class NewBorn {
    
        private int id;
        private String c_daughter;
        private String s_son;
        private String name;
        private int birthdate;
        private int weight;
        private int height;
        private int motherId;
        public NewBorn(int id, String c_daughter, String s_son, String name, int birthdate, int weight, int height,int motherId) {
            this.id = id;
            this.c_daughter = c_daughter;
            this.s_son = s_son;
            this.name = name;
            this.birthdate = birthdate;
            this.weight = weight;
            this.height = height;
            this.motherId = motherId;
        }

Of course I have Constructor, setters-getters and to string

I find it not easy to understand this. I do just need to extend one class to another ? like (extend Mother in the NewBorn class?) Can someone please explain how I can implement bidirectional full relation between two objects so further one I can say that (Mother may have many children, children has exactly one mother).

Upvotes: 1

Views: 134

Answers (1)

The IT Dejan
The IT Dejan

Reputation: 866

To have a full bidirectional relationship, a simplest form should be represented like this:

Class Mother

class Mother {
  private Set<Child> children;

  // getters, setters, constructors, rest of props
}

Class Child

class Child {
  private Mother mother;

  // getters, setters, constructors, rest of props
}

You can read more about it here.

Upvotes: 2

Related Questions