Mr.Sky
Mr.Sky

Reputation: 91

ArrayStoreException in java arrays

It is an example in the book Core Java

Two class Employee and Manager. Manager extends Employee. The code below

Manager[] mans ={new Manager("Adam"),new Manager("Ben")};              
Employee[] emps =mans ;   
emps[0] = new Employee ("Charlie");//throws ArrayStoreException in runtime actually;

The author said that in this situation the elements in arrays SHOULD REMAIN THE SAME TYPE or it throws ArrayStoreException. In my mind emps[0] is just a reference to instance 'new Employee ("Charlie")' and type of emps[0] is Employee declared before.So why it throws exception.Is there something wrong with my basics?

Upvotes: 4

Views: 927

Answers (3)

Pshemo
Pshemo

Reputation: 124275

When array is created it remembers what type of data it is meant to store. So if you have classes

class Employee { .. }
class Manager extends Employee { .. }

and you will create array

Manager[] arrM = new Manager[10];

array will remember that it need to store only instances of Manager class or its derived types. But such array can't store super type of Manager since super type may not have all methods or fields of Manager class so something like

arrM[0] = new Manager();

is OK, but

arrM[0] = new Employee();

throws java.lang.ArrayStoreException: Employee pointing that Employee is not correct argument here.

So in your case you are

  • creating array for Managers

    Manager[] mans ={new Manager("Adam"),new Manager("Ben")};
    
  • creating reference to this array using its super-type Employee

    Employee[] emps =mans;
    

    (but this reference it still pointing to array which can hold only Menagers)

  • and trying to place into array new Employee

    emps[0] = new Employee("Charlie");
    

but as I mentioned this can't be allowed because it is possible that Employee do not have same members as Manager. Lets say Menager can hire(...) someone, while Employee can't (doesn't have this method). What would happen if you would call

    mans[0].hire(new Employee("Tom");

and emps[0] = new Employee ("Charlie"); would not throw exception but would let you place Employee in emps[0]? Since mans and emps are using same array it would mean that mans[0].hire(new Employee("Tom") would be called from Employee("Charlie") which can't hire anyone because Employee don't have hire method.

That is why you can't place instance of super-type (Employee) in array of Managers.

Upvotes: 4

jhobbie
jhobbie

Reputation: 1016

It throws an exception because, like the book says, the types are different. Employee[] emps = mans; means that emps has two elements in it with type Manager. So if you add a new element or replace an existing element, it must be the same type.

I wonder if you made an array mans with size 1 (don't know what you would do this, maybe an ArrayList if you're planning to expand it later) and then followed the rest of your code, what would happen. I bet it would still not let you insert an Employee into the array.

Upvotes: 0

Paolo
Paolo

Reputation: 2472

On second line you can assign an array of subclass (Manager [] mans) to a reference to an array of superclass (Employee[] emps). No problems on that... but after this line the real type of emps is

Manager[]

The real type of the array is determined at run-time and it is worth nothing it is referenced by a reference to Employee[], it sitll is an array of Manager.

So emps[0] is expected to be of type Manager.

Upvotes: 0

Related Questions