Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Super class array object assigned with sub class array object

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();
    }
}
like image 713
Siva Avatar asked Nov 25 '14 06:11

Siva


People also ask

What happens when a subclass object is assigned to a superclass object in Java?

Therefore, if you assign an object of the subclass to the reference variable of the superclass then the subclass object is converted into the type of superclass and this process is termed as widening (in terms of references).

Can a superclass object reference a subclass object?

Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).

Can a superclass object be treated as a subclass object?

A superclass object is a subclass object. c. The class following the extends keyword in a class declaration is the direct superclass of the class being declared.

What happens if both superclass and subclass have a field with the same name?

When declaring a variable in a subclass with the same name as in the superclass, you are hiding the variable, unlike methods which are overwritten.


2 Answers

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.

like image 110
Sarthak Mittal Avatar answered Sep 22 '22 21:09

Sarthak Mittal


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.

like image 26
Eran Avatar answered Sep 23 '22 21:09

Eran