• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

ArrayStoreException pls explain?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.Serializable;
class Animal{

}
class Dog extends Animal{

}
class NewClass {
public static void main(String... a) {

Dog[] dogs=new Dog[10];
Animal[] animals=new Animal[10];
animals=dogs;
animals[0]=new Animal(); // ArrayStoreException
Animal m=new Dog();
m=new Animal();// No Exception


}
}

pls explain?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original code :
1.import java.io.Serializable;
2.class Animal{

3.}
4.class Dog extends Animal{

4.}
5.class NewClass {
6.public static void main(String... a) {

7.Dog[] dogs=new Dog[10];
8.Animal[] animals=new Animal[10];
9.animals=dogs;
10.animals[0]=new Animal();
11.System.out.println(animals[0]);
12.Animal m=new Dog();
13.m=new Animal();
}
}

The exception is due to line no 9.
if you make line no 9. a comment line the program will compile without errors.
Other wise at line no 10. if you call the Dog class constructor that is
animals[0] = new Dog();
the program will compile without error.

Thus assiging the reference typr dog to animals without inhertance has caused the error.


HOpe i am right......
 
Srikanth CS
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original code :
1.import java.io.Serializable;
2.class Animal{

3.}
4.class Dog extends Animal{

4.}
5.class NewClass {
6.public static void main(String... a) {

7.Dog[] dogs=new Dog[10];
8.Animal[] animals=new Animal[10];
9.animals=dogs;
10.animals[0]=new Animal();
11.System.out.println(animals[0]);
12.Animal m=new Dog();
13.m=new Animal();
}
}

The exception is due to line no 9.
if you make line no 9. a comment line the program will compile without errors.
Other wise at line no 10. if you call the Dog class constructor that is
animals[0] = new Dog();
the program will compile without error.

Thus assiging the reference type dog to animals caused the error.


Hope i am right......
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vikram,



Line 1: It is our preparation step to make the compiler fool before Line 1
Dog IS-A(n) Animal
So Animal reference variable (reference to one D array) can simply refer
to its subclass (reference of one D array).
Runtime type of the ani would be Dog.
Just check it out:

After Line 1 place this line:


Compiler could not recognize the Line 2, because it thinks as:
ani[0] can hold reference of the Animal object. Because it sees that
ani is reference varaible of type Animal;

But at the runtime when you try to store Animal object ani[0] ArrayStoreException occurs because it can't fit the parent to the child.





Regards,
cmbhatt
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic