2

Say I have one superclass, and several subclasses inheriting it. I'd like to cram all of them into the same Arraylist for easy interaction, because for this one specific interaction, im only messing with stuff they have in common from the superclass.

Is this doable, or will I absolutely need a separate Arraylist for each object type?

If it is, how?

If ArrayLists can't do this, are there any other operable collections that could?

2 Answers 2

5

you can create the arraylist as

List<YourSuperClass> list = new ArrayList<YourSuperClass>();
Sign up to request clarification or add additional context in comments.

Comments

1

I think what you're after is: ArrayList<? extends SuperClass>.

2 Comments

unfortunately you can't insert elements into a list defined this way.. This is a read-only definition.
BigFatGuy was right. Although, knowing that I can do this with arraylists will probably still be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.