Reputation: 607
I have fairly beginner to intermediate experience in object oriented programming. In general, why or in what situations do we design a class that has a collection of its own objects as its member? Is there a good example to understand this usage?
Upvotes: 0
Views: 44
Reputation: 814
Such a Class/Type is called a Recursive Data Type or Recursive Data Structure. A tree structure for example consists of a bunch of node objects, each of it containing a list of child nodes. See http://www.dreamincode.net/forums/topic/198160-data-structures-recursion-stacks-and-trees/
A suiting software design pattern to model such a recursive structure in Java is called composite. See https://dzone.com/articles/composite-design-pattern-java-0
Upvotes: 2