🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.help
Article #155

Re: Help -- Java Error

#155
From: Alexander Berndt
Date: Mon, 09 Aug 1999 00:00
38 lines
1754 bytes
Shashank schrieb in Nachricht <7olh27$sb$1@news.chorus.net>...
>Consider the following Java Program


>class A
...
>class B extends A
...

>       A[] array = new B[5];
>       array[0] = new B(10);
>       array[1] = new B(8);
>       array[2] = new B(6);
>       array[3] = new A(4); // line 23
>       array[4] = new A(2); // line 24
>       for(int i = 0; i< 5; i++)

...
>java.lang.ArrayStoreException:
>        at FinalC.main(FinalC.java:23)


the error is some lines above.

A[] array = new B[5];

you have a declared A-array, but it is implemented as an B-array. So only B
and subclasses of B are allowed. On the other hand, you get only A-objects
out of this array.

A[] array = new A[5];
this helps, now you can insert A-objects and get-out A-objects.

To understand this behavior, you should remember that array's are just
special objects.

Ciao, Alex
Technical Details
Message-ID: <7om0gt$16s$1@inet16.us.oracle.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <7olh27$sb$1@news.chorus.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10568c,fe1ae2c02024cf32
X-Google-Attributes: gid10568c,public
X-Google-Thread: 113ae9,fe1ae2c02024cf32
X-Google-Attributes: gid113ae9,public
X-Google-Thread: fb99b,fe1ae2c02024cf32
X-Google-Attributes: gidfb99b,public
From: "Alexander Berndt" <ab18@inf.tu-dresden.de>
Subject: Re: Help -- Java Error
Date: 1999/08/09
Message-ID: <7om0gt$16s$1@inet16.us.oracle.com>
X-Deja-AN: 510517960
References: <7olh27$sb$1@news.chorus.net>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
X-Complaints-To: usenet@inet16.us.oracle.com
X-Trace: inet16.us.oracle.com 934184285 1244 140.84.17.72 (9 Aug 1999 07:38:05 GMT)
Organization: Oracle Corporation. Redwood Shores, CA
NNTP-Posting-Date: 9 Aug 1999 07:38:05 GMT
Newsgroups: comp.lang.java.help,cz.comp.lang.java,de.comp.lang.java