6

I'm trying to assign a sub class object array to its super class. The program compiles successfully, but I' getting an ArrayStoreException. I know that arrays parent and child are references to same array, but shouldn't I be able to access method func 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();
    }
}
1
  • as proper java code, you're being very selective with your visbility. For uniform code, you want some public in there (and you want to fix that whitespace being all over the place... remember to read through stackoverflow.com/help/how-to-ask - the "remember to proofread" part isn't in there just for fun. Many people, you included, forget to) Commented Nov 25, 2014 at 6:24

4 Answers 4

4

You can't do this:

Cclass[] child = new Cclass[10];
Pclass[] parent = child;
parent[0]=new Pclass();

You should try doing this:

Cclass[] child = new Cclass[10];
Pclass[] parent = child;
parent[0]=new Cclass();

That's because, You first assigned the Pclass array to the child reference that can only have Cclass objects, then you are trying to assign Pclass object to the parent reference, that's not allowed!

See, what happens is that you have created a Cclass object on the heap when you wrote new Cclass, though the Cclass objects were null in the array but now they would accept only Cclass objects or it's subclass's objects

so assigning the Pclass object would be illegal!

Reason for getting a runtime exception and not compile time:

The compiler only checks whether the classes are in the same inheritance hierarchy or not, since they are in the same hierarchy you get a Runtime exception.

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

4 Comments

OP knows he can't do that - the question is why not? Your alternative suggestion has nothing to do with the question whatsoever. -1
@drewmoore it takes time to write the full answer :)
downvote rescinded, but you still haven't really answered this question. The underlying confusion is a reference-vs-value issue
because compiler doesn't checks that, what the compiler checks is whether those classes are in the same inheritance hierarchy or not, since they are in the same hierarchy you get a runtime exception, i think i should mention this in the answer too :)
1

If you read the spec of ArrayStoreException, you'd find out it is thrown 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 a Cclass array, so only instances of Cclass (or sub-classes of it) may be stored in this array. The fact that you store the reference of that instance in a variable of type Pclass[] doesn't change that.

Comments

0

Although the reference to the array is Pclass, they array object you are referencing is of type Cclass (you instantiated the object new Cclass[]. You cannot change the type of the object by having a variable of a different type reference it). You cannot store a Pclass object in a Cclass array.

Instead, if possible, you should create the object using the subtype:

parent[0] = new Subclass();

Comments

0

You have actually instantiated a child class object in the line:Cclass[] child = new Cclass[10]; and when you instantiate a parent object using new Pclass(); you create the parent object.When you assign it to child object refernce the downcasting occurs at runtime and it fails because you are trying to store parent object into a child reference.Hence it throws ArrayStoreException i.e you are trying to store the wrong type of object into an array of objects.

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.