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

  • SEARCH
  • Home
  • 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 6095997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:52:32+00:00 2026-05-23T12:52:32+00:00

I have a BinarySearchTree class which is built using a BinaryTree class. Now I

  • 0

I have a BinarySearchTree class which is built using a BinaryTree class. Now I want to build a RedBlackTree class by subclassing the BinarySearchTree class. The problem is that the BinaryTree class doesn’t have a field for color. So, I created a ColorBinaryTree class that’s a subclass of the BinaryTree class. Here’s where I get a little confused. In my BinaryTree class I have the following method

protected BinaryTree<E> parent(){
     return parent;
}

where parent is obviously another BinaryTree. In my RedBlackTree class, I will need to be able to access parents of ColorBinaryTree objects as well. However, I can’t just use the method inherited from the BinaryTree class because that returns a BinaryTree object which means I can’t access the color. With the following code I get an error

ColorBinaryTree<E> parent = newNode.parent();

where newNode is a ColorBinaryTree object. So it seems to me the only way to do it is to overwrite the above method in my ColorBinaryTree sublcass like so.

@Override 
protected ColorBinaryTree<E> parent(){
     return parent;
}

Am I missing some way to get aorund this or do I just have to go and override all of the methods that return BinaryTree objects? If so it seems kind of a waste since the body of the methods are exactly the same.

  • 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-05-23T12:52:32+00:00Added an answer on May 23, 2026 at 12:52 pm

    Is there a reason you’re not casting the result of the superclass’s parent function?

    Like this:

    ColorBinaryTree<E> parent = (ColorBinaryTree<E>)newNode.parent();
    

    Also kind of a pain, but probably what you’re looking for.

    The best way to deal with it, is to probably override all of the functions from your base class returning BinaryTree with a method that returns ColorBinaryTree. Then in the implementation of the child class call the parent’s method and perform the cast there before returning. That way you don’t have a million casts spread out in your code. It might look like this:

    @Override
    protected ColorBinaryTree<E> parent() {
        return (ColorBinaryTree<E>)super.parent();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
have a table that dynamically generates text boxes in run time. I want to
Have a build process that can't be edited and need to pack another war
Have some validating problems which seem to appear only when using the Auth component.
Have you ever experienced that the Bootstrap doesn't save (with flush or without flush
Have just started using Google Chrome , and noticed in parts of our site,
Have a slight problem. Trying to post XML to a server. To do this,
Have a problem with fscanf() - it does not skip to the next word
Have following Java code,that creates StringBuilder with \n,i.e. carriage return delimiters: while (scanner.hasNextLine()){ sb.append(scanner.nextLine()).append(\n);
have abit of a problem with Jquery UI dialog. I set it to modal:true,

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.