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.
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.
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
cmbhatt
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.