Junior — Middle
What steps are necessary to add three objects of different classes to a data structure of array type?
sobes.tech AI
Answer from AI
In Java, arrays are homogeneous data structures, meaning all elements must be of the same type. To add objects of different classes into a single array, you need to use a common base type, such as the Object class.
Example of adding three objects of different classes into an Object array:
Object[] array = new Object[3];
array[0] = "String"; // String
array[1] = 123; // Integer (autoboxing)
array[2] = new ArrayList<>(); // ArrayList
Thus, an Object array can contain objects of any class, since all classes inherit from Object.