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
you can create the arraylist as
List<YourSuperClass> list = new ArrayList<YourSuperClass>();
answered Apr 3, 2012 at 3:07
MeBigFatGuy
28.7k8 gold badges64 silver badges68 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
I think what you're after is: ArrayList<? extends SuperClass>.
answered Apr 3, 2012 at 3:07
smessing
4,3601 gold badge25 silver badges20 bronze badges
2 Comments
MeBigFatGuy
unfortunately you can't insert elements into a list defined this way.. This is a read-only definition.
Johnny Tarr
BigFatGuy was right. Although, knowing that I can do this with arraylists will probably still be useful.
lang-java