-3

I need to create a Parent and Child class but A child class might contain Parent class, How can I do that in java?

For example:

ParentClass {
  ChildClass childClass;
}

ChildClass {
  ParentClass parentClass;
}

is this possible? and if possible, what is the term in Java?

Joseph Move
  • 69
  • 1
  • 9
  • 3
    Your code doesn't show any inheritance - also what is the problem with having a reference to a `ParentClass`? – UnholySheep Mar 23 '18 at 13:29
  • it can be Parent has Child and Child has Parent and that Parent has child and that child has Parent – Joseph Move Mar 23 '18 at 13:30
  • 2
    You said that, but where is the problem? – UnholySheep Mar 23 '18 at 13:30
  • 3
    Did this compile and run? Did it do what you wanted? How would you go about using these objects? A clearer question with a real example will be easier to answer. – Josh Lee Mar 23 '18 at 13:32
  • You can compose classes with whatever you want. You'd have a problem if class1 inherited from class2 and class2 inherited from class1, but as UnholySheep mentionned your code shows no inheritance. – Aaron Mar 23 '18 at 13:32
  • Maybe my question is wrong, My scenario is: A have an atrribute of B but B can have an A and that A can have attribute B – Joseph Move Mar 23 '18 at 13:32
  • Your question seems right. It's just that the answer is : yeah, you can do that. No problem. (that's no parent/child relationship though) – Aaron Mar 23 '18 at 13:33
  • 4
    That's not a question. Did you actually try doing that? – Josh Lee Mar 23 '18 at 13:33
  • Yes you can do it but it probably isn’t a good idea. I don’t know if there is a specific name. It’s quite tricky to initialise the objects though.https://stackoverflow.com/q/19808342/6253321 – cpp beginner Mar 23 '18 at 13:37
  • 1
    Possible duplicate of [How to initialize a circular dependency (final fields referencing each other)?](https://stackoverflow.com/questions/19808342/how-to-initialize-a-circular-dependency-final-fields-referencing-each-other) – vincrichaud Mar 23 '18 at 13:39

4 Answers4

1

This is possible as the fields are null, creating one instance will never create its other classes instance.

The following will explode at run-time:

class ParentClass {
  ChildClass childClass = new ChildClass();
}

class ChildClass {
  ParentClass parentClass = new ParentClass();
}

new ChildClass();

The following will explode too.

class ParentClass {
  ChildClass childClass = new ChildClass();
}

class ChildClass extends ParentClass {
}

new ChildClass();

Or simply:

class ParentClass {
  ParentClass parentClass = new ParentClass();
}

new ParentClass();
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

The question does not refer to inheritance at all. It's just the use of "child" and "parent" name that are confusing.

You are making a circular dependency where class child has a field to reference a parent and class parent has a field to reference a child.

This question has already an answer here. The answer is either you use a factory to reference your field in the constructor :

class Parent {
    Child child;

    Parent(Child child) {
        this.child = child;
    }
}

abstract class Child {
    Parent parent;

    Child() {
        this.parent = constructParent();
    }

    protected abstract Parent constructParent();
}

public class Main {
    public static void main(String []args){
        new Child(){
            protected Parent constructParent(){
                return new Parent(this);
            }
        };
    }
}

Either you affect your field later using setters :

class Parent {
    Child child;

    Parent() {}

    void setChild(Child child) {
        this.child = child;
    }
}

class Child {
    Parent parent;

    Child(){}

    void setParent(Parent parent) {
        this.parent = parent;
    }
}

public class Main {
    public static void main(String []args){
         Parent parent = new Parent();
         Child chill = new BChild();

         parent.setChild(child);
         child.setParent(parent);
    }
}
vincrichaud
  • 2,218
  • 17
  • 34
0

Yes it is possible. You are looking for the term Composition has-a relationship.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

You are most probably confusing inheritance with composition.

In inheritance, a child class is a parent class (and more). It is produced with the extends keyword, as in the following code :

class Dog extends Animal { /*...*/ }

The Dog is an Animal and as such will inherit the parent class' public and protected methods, maybe such as feed(), breed(), etc. It can define other methods specific to the child class such as bark().

This direction must be unidirectional, if a class A extends a class B, the class B can't extend the class A (their definition would recurse into an infinite loop : "What is a Dog? Well, to begin with, it's an Animal ; alright, what is an Animal ? Well to begin with, it's a Dog, etc.")

In composition, a class has a reference to an instance of another class. It is produced by using fields that reference instance of other classes, such as in the following code :

class Kennel {
    Director director;
    Set<Dog> dogs;
    //...
}

A Kennel has a Director and a set of Dogs.

In this case the relation between both classes can be bi-directional (a Dog can have a Kennel field) without any problem, and the relation actually isn't between classes but rather between instances (except if your fields are static, but let's not bother with that).

Aaron
  • 24,009
  • 2
  • 33
  • 57