First of all i want to highlight:
Object is the direct super class for all the array types
whether it is single dimension array or multi dimension
or array of String or array of any other derived type.
I get confused with this code
String[][] str=new String[2][3];
Object[] obj=str;
This compiles fine. because Object is a super class String is a sub
class
here suppose we have
Object[] objArray = new Object[2];
objArray[0]= // it can hold something which is of type object
// as we know String[] IS A Object, so I can assign it here like below
String[] strArray = new String[3];
objArray[0] = strArray ;
here I am assigning a single dimension String array as element of objArray, so we can say objArray can hold 2d String array i.e String[][] i.e.
Object[] objArray = new String[2][];
but the following code produce errors
class Ex { }
class Ex1 extends Ex { }
class ExTest {
public static void main(String[] args)
{ Ex1[][] ex1=new Ex1[2][3]; Ex[] ex=ex1;
} }
but this code produce error why
it is because ex[0] can hold something which IS A Ex.
suppose if Ex[] ex=ex1; is allowed
then ex[0] should be able to hold 1D array of Ex1
ex[0] = new Ex1[3];// but this can not be possible as Ex1[] IS A Object NOT Ex