Beginner Array of Objects Confusion Beginner Array of Objects Confusion arrays arrays

Beginner Array of Objects Confusion


This is one of those weird things that Java allows you to do, assigning an array of a derived class to a variable of array of the base class.

In your code, x at compile time is of type Type1[]. That's what the compiler thinks it is. At runtime, x is of type Type2[], but the compiler does not know that.

The first error occurs at runtime because as you said, you can't assign Type1 to a variable of type Type2.

But the second error occurs at compile time because the compiler still thinks that x is of type Type1, and there is no method called method2 in Type1, even though x actually holds a Type2[] at runtime.

To call method2, you need to tell the compiler that x[1] is of type Type2 by casting:

((Type2)x[1]).method2();

Lesson of the day? Don't do this:

Superclass[] array = new Subclass[2];

You'll get yourself in trouble.


A super-type reference can refer to a sub-type. I think you understand this, since the first error is coherent to you.

The second error stems from the fact that at compile time, x is still a Type1[]. This means that at run-time the reference can hold any sub-type, including a type that doesn't have the method method2. That's why you can only use the methods defined in Type1.

You could, for example, check whether the type is actually Type2 at runtime using isInstanceOf, and then cast it to a Type2 and then use method2.There are usually better solutions, though.


to put it in layman terms for the below code,

Type1[] x = new Type2[2];

let us just take Type1 x = new Type2; not arrays, just classes.

What do you infer from the above step?

Here x is a reference variable. We are creating an object of "Type2" and x has the reference of the Object Type2 [lets just say, x is a remote control pointing to Type2 object]. Dont forget that, x is of the type 'Type1', which means it does not have a 'method2()' button on its remote control.[Remember, method2 is of Type2 Class not Type1].

Compiler calls a method on an object,only when it sees it inside the class. compiler does not care which Object you are creating [here: new Type2], all it cares is on whom you are making the call (here: x[1], which is of type Type1). So, it throws the compile time error. It does not bother what happens at runtime or which object it is pointing to, all it cares is if Whether the reference type has the calling method or not [In Our terminology, does the remote control has that button]?.

why is the first error a runtime error while the other is a syntax error?

I hope the above explanation, (when you consider Array types as per your question) answers your second half of the question.

And, as for the first part, you pretty much answered it. Anyhow, thing is Arrays allow this kind of stuff and blows up at Runtime whereas, Arraylist do it at compile time.[That is a part of Generics].

As you said,you are new to this stuff, here is my reference for You.

Reference:

Head First Java