Multidimensional arrays in Java extends which class?

arrays, java, multidimensional-array

Solution

A multidimensional array in Java is really just an array of arrays (of arrays)* .

Also, arrays are considered subclasses of Object.

So, your `int[][]` is an `Object[]` (with component type `int[]`), and also an `Object` (because all arrays are objects)

An `int[]` however is not an `Object[]` (but it is still an `Object`).

So it seems that two dimensional arrays extend Object[]

I am not sure if "extend" is the proper word here. Arrays have a special place in the Java type system, and work a little different from other objects. A two dimensional array is definitely an Object[]. But if you are asking about superclasses, the only superclass that any kind of array has is Object. All arrays are also Cloneable and Serializable.

Problem

I need to know which class multidimensional arrays in Java extends exactly? When we assign ``` Object[] ref=new int[]{1,2,3}; ``` the compiler complains that the objects are of different types. So it seems that one dimensional arrays extend `Object`; I know that already. But when we assign ``` Object[] ref2=new int[][]{{1,2,3},{4,5,6}}; ``` the compiler will not complain. So it seems that two dimensional arrays extend `Object[]`. But when I print its superclass name: ``` System.out.println(ref2.getClass().getSuperclass().getName()); ``` I got `java.lang.Object`. So can anyone explain what's going on here?

Original source