A example in book 《Core Java Volume I》
public class Employee {
...
}
public class Manager extends Employee {
...
}
Manager[] managers = new Manager[3];
Employee[] staff = managers; //OK
staff[0] = new Employee(); //error
staff[0]will cause ArrayStoreException, beacuse the array store a wrong object.
I give the memory diagram of the array:
I think variable staff store the address of staff[0](like c++, where in int *p = new int[3], variable p stores the address of the first element of the array).
Because staff is equal to managers, so they point to the same position of memory, when you write the statement staff[0] = new Employee(), I think it is equal to the statement managers[0] = new Employee(), the reference to parent points to the child object is wrong.
I'm not sure if I'm right, I hope to know the relationship of the reference to array (staff/managers) and the individual array elements (staff[0], staff[1]...), whether staff can tell me the position in memory of the first element in array.
And I would also like to know, for different arrays of objects, like Cat[] cats = new Cat[2] and Dog[] dogs = new Dog[2], whether they take up the same size in memory. Do references to different objects take up the same amount of memory?