Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:17:36+00:00 2026-06-02T21:17:36+00:00

I have a superclass and a subclass as follows: class Tree{ .. public void

  • 0

I have a superclass and a subclass as follows:

class Tree{
 ..
 public void add(..){
 //makes a call to protected function add(..) 
 }//for client to use.
 protected TreeNode add(..){}//recursive function which calls itslef
}

class Stree extends Tree{
 //overrides the recursive add function from class Tree
 protected TreeNode add(..){
   ..
   super.add();//calls the non-recursive add function in superclass.
 }
}

The problem here is that when I call super.add() from the new add function in the subclass, it goes to Tree.add(). Inside Tree.add(). there is a call to add(), which calls the recursive add function in the subclass instead of super, i.e. Stree.add(), instead of Tree.add() which results in an infinite loop. Do you guys see where the problem is?

This is a homework assignment hence I cannot change the name of the recursive function. I am explicitly required to add functionality to the recursive add function, without rewriting any existing code, which basically means I will have to make a call to the original add() function.

edit: Code for the Tree.add()//recursive. Note that I cannot modify this code to gain the functionality I seek.

protected StreeNode add(StreeNode node, String value) {
        if (node == null) {
            node = new StreeNode(value);
            numElements++;
        } else if (node.data.compareTo(value) == 0) {
            // do nothing, String was already in Set
        } else if (node.data.compareTo(value) > 0) {
            node.left = add(node.left, value);      // x = change(x)
        } else {
            node.right = add(node.right, value);    // x = change(x)
        }

        return node;
    }

edit: Now that I see that this is the expected behaviour, how do I go about achieving the following:

  1. Add the value using the original recursive add()
  2. Implement extra functionality
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:17:38+00:00Added an answer on June 2, 2026 at 9:17 pm

    Without seeing the parameters I assume void add(...) is the method to add something into the tree, whereas the protected recursive method looks for the node to add to and then performs the add.

    I further assume the public non-recursive method passes the tree’s root to the recursive method as a start parameter while the recursive method either passes the left or right child until you hit a leaf. Calling the non-recursive method might thus in starting at the root again and again.

    Thus I’d say that the recursive and inherited method should not call the non-recursive version but should call itself again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a super class: public class SuperClass { public void dosomething() { firstMethod();
I have a genericised class that I wish to subclass as follows: public class
I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have a method that is expecting a List<SuperClass> as argument: public void myMethod(List<SuperClass>
I have a function defined in a superclass, InteractorStyle. Then I have another class
I have 3 classes A,B and c as follows A.java class A { protected
I have the following code: class SuperClass { public static String getName() { return
I have a subclass that calls a method from a superclass. The method in
I have an object of a subclass extending its superclass. There is an overridden
If subclass in objective-c wants to override a super class's method, does it have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.