Different References in Same Array

I have two classes in which objects I’m creating are from. I’m looking for some sort of array that will store references to these objects but I don’t know what type the array should be. So say this is what I have: ArrayList<Class1> visableObjs = new ArrayList<Class1>(); for Class1. If I have an object from Class2 can I do this in the same array or not?

Popular White Paper On This Topic

Hi, to do this Class1 and Class2 must have a common super class. Your ArrayList must be initialized to hold objects of the super class. For instance, Object is the common super class of all classes. Therefore, if you initialize your ArrayList for Object you can store objects of Class1 and Class2 in it. But note you are skipping type safety when doing this.