Skip to main content

Arrays of superclasses and subclasses Help!

1 reply [Last post]
foxmulder900
Offline
Joined: 2006-10-18

say I have a class 'A' and a class 'B' which extends A. I want to make an array with instances of both class A and B but I keep getting an ArrayStoreException.

example:

public class A
{
}

public class B extends A
{
}

A[] Array;
Array[0] = new B();

Any Suggestions?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
forax
Offline
Joined: 2004-10-07

It depends of thedynamic type your array :

[code]
A[] array=new A[5];
array[0]=new B(); // ok
array[0]=new A(); // ok

A[] array2=new B[5];
array2[0]=new B(); // ok
array2[0]=new A(); // ArrayStoreException
[/code]

Rémi Forax