You cannot do this:
Cclass[] child = new Cclass[10]; Pclass[] parent = child; parent[0]=new Pclass();
You should try:
Cclass[] child = new Cclass[10]; Pclass[] parent = child; parent[0]=new Cclass();
This is because you first assigned the Pclass array to the child link, which can only have Cclass objects, then you are trying to assign the Pclass object to the parent link, which is not allowed!
See what happens when you create a Cclass object on the heap, when you write a new Cclass class, although Cclass objects were empty in the array, but now they will only accept Cclass objects or subclass objects
so assigning a Pclass object would be illegal!
The reason for the exception being thrown at runtime rather than compilation time:
The compiler only checks if the classes are in the same inheritance hierarchy or not, since they are in the same hierarchy, you get a Runtime exception.
source share