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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:10:23+00:00 2026-05-27T19:10:23+00:00

I may have the wrong idea with this but bear with me… I have

  • 0

I may have the wrong idea with this but bear with me…

I have a function that I want to call with one of multiple sub-class types ComboA, ComboB, etc (all extending the superclass “Combos”).

    ArrayList<Combos> combos = null; //of type combos, which is super class of Combo A and Combo B
    switch (which) {
        case 1:
            combos = CombosA; //this is of type ComboA
            break;
        case 2:
            combos = CombosB; //this is of type ComboB
            break;
}

Depending on the switch statement, I want a single variable in the function (combos) to retain all the class functions of the sub-class. This is so I don’t have to make a bunch of functions for each Combo type -I’d like to use the same variable name (combos) regardless of which subclass it is.

I tried using generics, but the thing is, the CombosA and CombosB variables are defined outside of this scope, global to the class, and I don’t know how to use generics unless I define the function as

public <E extends Combos> void Test(ArrayList <E> CombosA)

Or something like this, which I guess would require another separate variable to be sent in the function call that wasn’t global. I might be able to copy the CombosA array list and send the copy into the function to get the generic function to work but, I thought there must be a better way.

Any help would be greatly appreciated!

  • 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-27T19:10:24+00:00Added an answer on May 27, 2026 at 7:10 pm

    It depends on what you next want to do with the selected “combo“. Since ComboA and ComboB are subclasses of Combos ideally you would just call a “Combos” method to perform a common action on each combo. Each subclass can then perform it’s own implementation of the function.

    For example, if you are going to do some sort of update on ComboA or ComboB etc, then there could be an update() method (possibly abstract) in the Combos class. ComboA has its version of update() and ComboB has its version of update(). Your loop then just loops over the combos calling the update() method knowing that each will do the correct thing without having to specify type.

    Does that help?

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

Sidebar

Related Questions

--EDIT-- I believe this is a valid question that may have multiple answers (as
This may be a doozy, but does anyone have an idea how to: Pass
This question may have been asked before, but I had trouble finding an answer,
I may have the wrong pattern here, but I think it's a fair topic.
This may be the wrong approach, but as I dig deeper into developing my
I may be barking up the wrong tree... However, what I have is a
Firstly, Modifying may be the wrong term, I see a few people have posted
You may have noticed that we now show an edit summary on Community Wiki
You may have heard of PawSense , a Windows-only utility that prevents keystrokes from
I have a CSV data file with rows that may have lots of columns

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.