my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about ArrayStoreException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about ArrayStoreException" Watch "Doubt about ArrayStoreException" New topic

Doubt about ArrayStoreException

Vishal Malu

Joined: Jun 05, 2005
Posts: 1
I came across this example in JLS 3.0 page 328

class Point { int x, y; }

class ColoredPoint extends Point { int color; }
class Test
public static void main(String[] args)
ColoredPoint[] cpa = new ColoredPoint[10];
Point[] pa = cpa;
System.out.println(pa[1] == null);
pa[0] = new Point();
catch (ArrayStoreException e)
produces the output:

I didn't understand why pa[0] = new Point(); gives the ArrayStoreException.
Can nebody explain it??
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 812
this is because the array is actually of type ColoredPoint which is now reference by superclass array type...but note that ..only refence is changed

inside the array all the reference index element are of point type and can only refer to opjects of ColoredPoint or any Subclass of Colored point..

if you get the point of last line... then fine...
else pls read inheritence....

as reference variable can refer to the object of type its declared or its subclass type...

Thanks and Regards, Amit Taneja
I agree. Here's the link:
subject: Doubt about ArrayStoreException
It's not a secret anymore!