Johnny Tarr
Johnny Tarr

Reputation: 23

Arraylist of subclasses in java

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?

Upvotes: 2

Views: 2658

Answers (2)

MeBigFatGuy
MeBigFatGuy

Reputation: 28623

you can create the arraylist as

List<YourSuperClass> list = new ArrayList<YourSuperClass>();

Upvotes: 5

smessing
smessing

Reputation: 4350

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

Upvotes: 1

Related Questions