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 6800657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:59:47+00:00 2026-05-26T18:59:47+00:00

I have an interface Tree and an abstract class RBTree which implements this interface.

  • 0

I have an interface Tree and an abstract class RBTree which implements this interface. I also have several classes Tree1…Tree9 which extend this abstract class.

I’ve written a test unit where i want to do something like this:

public void testRandom(RBTree tree){
    for(int i = 0; i < 10; i++){
        rbTree = new Tree1(); //if the tree in the parameter was of instance Tree1
        rbTree = new Tree2(); //if the tree in the parameter was of instance Tree2
        //etc.

        /**
         * do something with rbTree
         */
    }
}

Is it possible to do this without using a chain of if-statements (or a switch) with a lot of instanceof() ?

(note: i can’t change anything about the design, i know it’s not really optimal)

  • 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-26T18:59:48+00:00Added an answer on May 26, 2026 at 6:59 pm

    You can use tree.getClass().newInstance().

    In case you want to have some more complex logic than instantiation, you would either need the instanceof approach, or better – make each RBTree subclass have a method that performs that logic (and make that method abstract in RBTree)

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

Sidebar

Related Questions

If I have interface IFoo, and have several classes that implement it, what is
I have a interface documented like this: typedef struct Tree { int a; void*
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Have a interface class abc { public: virtual int foo() = 0; ... }
I have an interface that I have defined in C++ which now needs to
I have a user interface with a tree view on the left, and a
I am working with tree's in java and I have the following interface for
I have a JTree which I give objects that implement the TreeNode interface, and
I have a third-party, closed source application that exports a COM interface, which I

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.