Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayStoreException pls explain?

 
vikram choudhary .
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Srikanth CS
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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......
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic