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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:14:04+00:00 2026-06-13T19:14:04+00:00

I need to have some sort of data structure that can contain heterogenous subclasses

  • 0

I need to have some sort of data structure that can contain heterogenous subclasses of the same superclass, all of which I have implemented myself.

So far, I am attempting to have an ArrayList<SuperClass> list = new ArrayList<SuperClass>();
and then, I am assuming I will be able to cast each slot of list into either of the subclasses, but this is not working out so well.

I need an efficient way to do the aforementioned.

Thanks!

  • 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-13T19:14:05+00:00Added an answer on June 13, 2026 at 7:14 pm

    You can do it with any data structure that exists, I would recommend a List or a Set. For instance:

    Collection<Super> supers = new ArrayList<Super>();  
    

    Now when you say this:

    I am assuming I will be able to cast each slot of list into either of
    the subclasses,

    That is an invalid assumption. The collection will hold any object that extends Super however you cannot arbitrarily cast each element into whatever you want. You would need to do an instanceof test on each element if you are looking for that type of functionality, example follows:

    for(Super currentSuper : supers)  
    {  
        if(currentSuper instanceof SubA)  
        {  
             SubA subA = (Suba) currentSuper);  
             // do stuff with subA
        }  
        else if(currentSuper instanceof SubB)  
        {  
             SubB subB = (SubB) currentSuper);  
             // do stuff with subB
        } 
    }  
    

    Scope as need be.

    Now on the point of Vlad:

    and much better design would be not to test what the actual class is,
    but just to call a virtual method, which will do the right thing in
    any case

    If you can guarantee the functionality of all potential sub-classes and have no issues with people overriding your classes (in the event you haven’t marked them final) you do not need to do the instance of test. Instead your code could be as simple as:

    for(Super currentSuper : supers)  
    {  
        currentSuper.doSomethingNifty();
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data and need to create a json file with this structure
I have some files that need to be sorted by name, unfortunately I can't
I have some data which i need to make some statistics. I need to
I need a data structure of some sort to do the following: One set
I have a data structure in my C++ program that has some attributes of
I have some sort of recursive function, but I need to parse a string,
I need to sort then print the result increasing and decreasing. I have some
I have some text that I always need to display (like a HUD). When
I have some HTML that I need to toggle between two structures - one
I am trying to have some sort of Data Object (I'm thinking a dictionary)

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.