4

I am working with inheritance while i encounter such problem. my code is as follows

   public class Parent {
        public void methodParent() {
            System.out.println("Parent");
        }
   }
   public class Child extends Parent {
    public void methodParent() {
        System.out.println("override method in Child");
    }

    public void methodChild() {
        System.out.println("method in Child");
    }
   }
   public class MainTest {
    public static void main(String[] args) {
        Child[] c = new Child[10];
        Parent[] p = c;

        p[0] =  new Parent();
        c[0].methodParent();
    }
  }

stack trace is

Exception in thread "main" java.lang.ArrayStoreException: com.test.Parent
    at com.test.MainTest.main(MainTest.java:10)

when i debug inspect c then i got message like

org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.

please help me to understand where is the problem.

2
  • 1
    Thanks for your valuable input, I understand that ArrayStoreException but now i just update my Parent class to interface and child implement Parent according to all of you code works fine but when i debug inspect c then i got message like org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array. please help why this error occurs at runtime and resolves automatically. Commented Jan 1, 2014 at 11:14
  • org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array. Why it gives me this error while debugging?? please explain!!!! Commented Jan 2, 2014 at 13:55

4 Answers 4

2

When you do this

    Child[] c = new Child[10];
    Parent[] p = c;

you are telling the compiler that p is just an array of Parent. However, there is also a runtime check that the array still must contain Child references. It is this runtime check which fails.

Sign up to request clarification or add additional context in comments.

Comments

2

See ArrayStoreException:

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

 Object x[] = new String[3];
 x[0] = new Integer(0);

That's exactly what are you trying to do. And that's not right. You're getting the exception in the line:

p[0] =  new Parent();

Here, you are trying to assign to p an instance of Parent, although it must contain Child according to the assignment you did before.

It's exactly like the example shown in the official docs, Parent is Object and Child is Integer.

1 Comment

Oh my twin brother :)
1

the actual type of the array which is assigned to variable 'p' is Child[]. Therefore this array can only 'store' instances of child objects. You are trying to store a parent to this array which is why the 'array store exception' is being thrown.

assigning a Child[] to a variable type of Parent[] is fine since child is a parent (as per your model). However the actual type of the array is checked at runtime by the jvm when it comes to storing objects inside the array.

Comments

1

From docs

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

 Object x[] = new String[3];
 x[0] = new Integer(0);

This what exactly happend at Parent[] p = c;

How ever you should consider using interfaces :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.