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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:08:40+00:00 2026-06-15T12:08:40+00:00

I have Four classes A, B, C and Test . Test is used for

  • 0

I have Four classes A, B, C and Test. Test is used for testing purposes.

B inherits from A. and C inherits from A.

I have a method called check() in the test class. It should return either B or C objects. So i am hoping to add B and C objects in an array, and call a random number between 1 and 2. So one of these 2 will be returned. I am not sure how to add B and C to an array. Can someone show me how to do this in Java ?

public Test{

public A check(){
  // add new B() and new C() to an array
  // call a random number to return element 1 or 2 in the array so it will either return B or C

  return either B or C;
}


}
  • 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-15T12:08:41+00:00Added an answer on June 15, 2026 at 12:08 pm

    Even better like this:

    public class Test{
        private static Random r = new Random();
        private static A[] arr = new A[] {
            new B(), 
            new C()
        };
        public static A check(){
            return arr[r.nextInt(arr.length)];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four classes, let's call S1, S2, S3 and S4. These class are
I have got four classes A , B , C and D . Class
I have the following PHP Classes class.property.php class.location.php class.amenity.php class.category.php all four classes handles
I have four classes. Class Person , and three more, Student , Professor ,
This is Java, using BlueJ. I have four classes called Person, Letter, Address and
Say you have class A, which classes A1, A2, and A3 inherit from. All
I have the following four classes: DataConsumer, DataProducer, SomeQualifier, a META-INF/beans.xml and a test.
I have four classes. Project Class, Student Class, ProjectFile Class and ProjectFrame JFrame. The
I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property
I have a four classes: class A(models.Model): field1 = models.BooleanField(default = True) class Meta:

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.