Java: superclass array object assigned by sub class array object

I am trying to assign an array of subclass objects to my superclass. The program compiles successfully, but I get an ArrayStoreException . I know that the parent and child arrays are references to the same array, but should I not have access to the func method at least?

 class Pclass { Pclass() { System.out.println("constructor : Parent class"); } public void func() { System.out.println("Parent class"); } } class Cclass extends Pclass { Cclass() { System.out.println("Constructor : Child class"); } public void func2() { System.out.println("It worked"); } public void func() { System.out.println("Common"); } } public class test { public static void main(String ab[]) { Cclass[] child = new Cclass[10]; Pclass[] parent = child; parent[0]=new Pclass(); parent[0].func(); } } 
+5
source share
4 answers

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.

+4
source

If you read the ArrayStoreException specification, you will find that you selected it to indicate that an attempt has been made to store the wrong type of object into an array of objects.

You created an instance of the Cclass array, so only Cclass instances (or its subclasses) can be stored in this array. The fact that you store the reference for this instance in a variable of the Pclass[] type does not change this.

+1
source

Although the reference to the Pclass array, the object of the array you are referring to is of type Cclass (you created an instance of the new Cclass[] object. You cannot change the type of the object by specifying a variable of a different type of link). You cannot save a Pclass object in a Pclass array.

Instead, if possible, you should create an object using a subtype:

 parent[0] = new Subclass(); 
0
source

In fact, you created the object of the child class in the line: Cclass[] child = new Cclass[10]; , and when you instantiate the parent using new Pclass(); , you create a parent object. When you assign it to a child of a refernce, downcasting happens at runtime, and this fails because you are trying to save the parent object to a child link. It is he who throws an ArrayStoreException , that is, you are trying to save the wrong type of an object into an array of objects.

0
source

Source: https://habr.com/ru/post/1207639/


All Articles