0

Is it possible to implement composition in Java? If we create a class A and use an instance of this class in class B, then how can we ensure that A cannot create an independent object by itself?

Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60
  • so based on the replies that i am getting i can safely conclude that it is not possible to implement composition in Java except through the use of inner class (which is way too obvious). Therefore a third party class can never be included as composition. – Farrukh Chishti Aug 21 '13 at 07:38
  • If you have a third party class for which you "cannot create an independent object", how can you use it in composition? – Amit Khanna Aug 21 '13 at 07:42
  • @AmitKhanna: Of course i can create an object of the third party class. I just want to confirm that the third party class can never be used in composition (whether i can make an object of it or not). – Farrukh Chishti Aug 21 '13 at 07:45
  • 1
    Why do the replies mean that it is not possible to implement composition in Java? Even if an instance of class A can be created outside of B how does that violate composition? – Viktor Seifert Aug 21 '13 at 07:47
  • Aggregation - referenced object can live on it own, composition - object cannot live outside of its parent. E.g. you can agregate glasses and be composed of two hands. – Leos Literak Aug 21 '13 at 08:35
  • @ViktorSeifert: It violates composition because in composition the child object cannot exist without the parent object. – Farrukh Chishti Aug 21 '13 at 08:50
  • @qualtar I don't think it is necessary to enforce that an instance of A cannot be created outside of B. It can certainly useful, in some scenarios, but it is also possible for A to be used as a part of another class aside from B. – Viktor Seifert Aug 21 '13 at 11:17
  • @ViktorSeifert: if A can exist without B then it would not be composition it would simply be aggregation. The object of A has to die if the object of B dies to qualify the relationship as composition. – Farrukh Chishti Aug 21 '13 at 11:23
  • @qualtar Would it solve your problem if A was a package-protected class in the same package as B? – Viktor Seifert Aug 21 '13 at 11:28
  • @ViktorSeifert: No. See, my point is that the object of A MUST die when object of B dies. Even for a package protected class the object of A can be used in other classes of the same package. – Farrukh Chishti Aug 21 '13 at 11:31

5 Answers5

2

I do not think it is possible. The only similar solution is to create A as private inner class of B. Then nobody else than B can instantiate A.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
1

You could have your composed classes as an inner class of your container class.

class ContainerClass{

    class ContainedClass1{

    }

    class ContainedClass2{

    }

}

This way they are tied to an instance of ContainerClass and cannot be created without an instance of the same. You can make them private, then they can only be created within the class

Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

If I understand you question correctly, what you want is an inner class. Essentially to create an inner class you write class A inside class B.

TheThirdOne
  • 427
  • 3
  • 10
0

You can create class A as static inner class of B and keep the constructor of A as private. Then only B can use this constructor.

Some links for reference:

scope of private constructor in Nested Class

Community
  • 1
  • 1
Amit Khanna
  • 489
  • 4
  • 16
0

Maybe you can use classLoaders to determine if A class loaded from B or not

Evgeny Makarov
  • 1,417
  • 1
  • 21
  • 41