Hi
I am having problems in getting Composition ?
pls tell about it.

thanks
Manoj

Dani AI

Generated

Following ’s question and the helpful pointers from , and , here is a concise, practical guide on recursion: when to use it, how to design it, and common pitfalls. Recursion is an algorithmic technique that fits problems that break down into smaller, similar problems. It is independent of whether your classes use composition or inheritance, though it often appears when you traverse composite/tree-like object graphs.

Prefer recursion when the problem has natural self-similarity: tree or graph traversals, divide-and-conquer algorithms (merge sort, quicksort), backtracking (permutations, N-queens), parsing nested structures, or when recursion makes code much clearer. Avoid recursion for very deep or performance-critical loops unless you can guarantee small depth or apply memoization to prevent repeated work.

A simple recipe to write a safe recursive method:

  1. Identify the base case(s) — the smallest input you can solve directly.
  2. Ensure each recursive step moves the state toward that base case.
  3. Keep mutable state out of shared globals; pass necessary state in parameters.
  4. Combine results from recursive calls into the final answer.
  5. Analyze complexity; add memoization or convert to iterative if calls explode.

A compact Java pattern for traversing a binary tree:

void traverse(Node node) {
    if (node == null) return;        // base case
    process(node);
    traverse(node.left);
    traverse(node.right);
}

Troubleshooting tips: missing or incorrect base cases are the most common bug. Watch for exponential recomputation (use memoization) and stack overflow on deep recursion — Java does not reliably perform tail-call optimization, so convert to an iterative solution with an explicit stack for large depths or increase JVM stack size only as a last resort. Recursion works equally well inside classes created by composition or inheritance and can be combined with polymorphic method calls when appropriate.

Recommended Answers

All 5 Replies

Composition is the act of taking two smaller items and putting them together to make a larger item.

Like a door handle being added to a door frame makes a door.

Regards,

Nate

Composition (called “has-a) is a relationship between classes where one class has a data member that is an instance of the other class, such as “a car has a tire. When creating a program, you need to choose whether to relate classes through inheritance or composition.


king

Composition (called “has-a) is a relationship between classes where one class has a data member that is an instance of the other class, such as “a car has a tire. When creating a program, you need to choose whether to relate classes through inheritance or composition.


king

hi all,

with regard to composition and inheritance, there is a term named as polymorphism...is it related to the above? so infact, composition is inheritance, but in my class, we usually called the method as inheritance and the sub-classes have the attributes and properties of the main class. i mean from the main-class, u will have the sub-classes and the relationship is called inheritance. hope that what i understand can be of use to you. cheers! :D

hi all,

with regard to composition and inheritance, there is a term named as polymorphism...is it related to the above? so infact, composition is inheritance, but in my class, we usually called the method as inheritance and the sub-classes have the attributes and properties of the main class. i mean from the main-class, u will have the sub-classes and the relationship is called inheritance. hope that what i understand can be of use to you. cheers! :D

Polymorphic behavior is more of a "is-a" relationship then a "has-a" relationship. A Circle is a type of Shape and should be able to be viewed as a Shape and have all the same behavior (methods) as Shape. Polymorphism is accomplished by both inheritance and interfaces in java.

Regards,

Nate

Hello To all who had make me to understand the basic Funda of Composition.

Can any of u please tell me where to use RECURSSION IN PROGRAM.
or WHEN THE PROGRAM DEMANDS IT. :?:


regards

Manojsah

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.