4

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

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

4 Answers 4

3

It compiles because obj will now hold two one-dimensional String[] objects.

All the below will compile:

String[][] str = new String[2][3];
Object obj1 = str;
Object[] obj2 = str;
Object[][] obj3 = str;
Sign up to request clarification or add additional context in comments.

3 Comments

but String[] str1=str; gives an error because we are assigning two dimensional array of String to one dimensional array ..but Object has exception why ..i still confused
Because Object is the ultimate super class. String is a subclass of Object.
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
1

It makes sense when you realize that java arrays, no matter how many dimesions they have, are ... Objects in the terms of IS-A relationship. So array of objects can hold pretty much any array since every array is an Object.

Comments

1

All array instances are also instance of java.lang.Object. Therefore all below assignments are legal. Elements of objArr1 array are one dimensional String array.

     Object obj1 = new String[3];
     Object obj2 = new int[3];
     Object obj3 = new String[3][3];
     Object[] objArr1 = new String[3][4];

UPDATE :

class Ex { } 
class Ex1 extends Ex { } 
class ExTest {
 public static void main(String[] args) { 
    Ex1[][] ex1=new Ex1[2][3]; 
    Ex1[][][] exArr=new Ex1[2][3][3]; 
    Object obj = exArr; //valid
    Object objArr[] = ex1; // valid assignment. every array instances are also object
    Object[][] obj2Arr = exArr;  // valid 
    Object[][][] obj3Arr = exArr; //valid
    Obejct[][][][] obj4Arr = exArr; //compiler error. obj4Arr is 4 dimensional, whereas exArr is three..
    Ex[] ex=ex1;  // compiler error
    Ex1[] ex1Arr = ex1 ; // compiler error
  } 
} 

You can not assign different dimensional array to each other, even if their element types are the same.. You can assign any array with any dimension to an Object array whose dimension is less than or equal to assigned array dimension. Because every array instance is also Object instance..

3 Comments

yes, but Ex1[] is assignable to Ex[], but not to Ex[][] or Ex.
i am sorry for asking again and again ..i thought this reflects is a relation ship between super and subclass..this works with Object but no other super class..
no, it is not relationship between super and subclass. You should accept this fact,"every array no matter how many dimension or whatever element type is Object " as an axiom. I would recommend you to read related part of JLS. java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
0

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

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.