954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How is an Object reference assignable to a subclass' object here ?

class hello 
{
   
}
class hello1
{
   public static void main(String aa[])
   {
      hello ob[]={new hello(),new hello(),new hello()};
      hello ob1[]=ob.clone();           //  1
   }
}


ob is an array which is also treated as an object, and hence has a default clone method which is called at (1). But the default clone method returns an Object reference, so that should not be assignable to an array of type hello. But its being assigned here to an array of hello type without errors. Why ?

daudiam
Junior Poster
152 posts since Dec 2009
Reputation Points: 2
Solved Threads: 2
 

Is this some more of the autoboxing type of help the compiler gives us?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Because arrays get special treatment as per the specs :
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
ob is an array which is also treated as an object, and hence has a default clone method which is called at (1). But the default clone method returns an Object reference, so that should not be assignable to an array of type hello. But its being assigned here to an array of hello type without errors. Why ?

An array is-an Object and therefore you can call methods of the Object class on an array, which inherits these methods. However, as you can tell by writing a short program, the array class overrides the clone method. Therefore when you call clone() on an array, it does not simply return the same Object reference, but it actually creates another array.

Hello ob[]={new Hello(),new Hello(),new Hello()};
Hello ob1[] = ob.clone();
if (ob == ob1) System.out.println("Same");
else System.out.println("Not the same");


The above code prints not the same, which to me, indicates that a new array Object has been created. The exact details of what the clone() method does for the array class I don't know, or really care. You will note, however, that if you print out

if (ob[0] == ob1[0]) System.out.println("ob the same");


it will indeed print out that they're the same, which is because in that case, your Hello class does not override clone, so it just returns the reference.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
Because arrays get special treatment as per the specs :

Why is that special treatment? Because they actually overrode clone..? Do they not usually do that?

edit: jumping ahead of myself, trying to catch you before you got out of the thread.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
Why is that special treatment?


Because arrays themselves are special. For e.g., every class which supports cloning (implements Cloneable) provides a public implementation for the clone method which shows up when you iterate through the methods of that class.

for(Method m : ArrayList.class.getMethods()) {
    System.out.println(m.getName());
}

prints out the name "clone" because ArrayList implements Cloneable and provides an implementation of the clone() method. This is again true for every other class which implements Cloneable except for arrays. I.e.

for(Method m : String[].class.getMethods()) {
    System.out.println(m.getName());
}

does not print out the "clone" method even though the Javadoc of the "clone()" method of Object class specifically says:[...] Note that all arrays are considered to implement the interface Cloneable[...]
Notice the play of words here; it says "considered" and not "actually" implement the Cloneable interface. I guess this was expected for a language construct which can be assigned to a reference type but doesn't have a programmer accessible class. I'm sure carefully thinking about how arrays are used in Java would give you a few more points as to *why* arrays in Java are special. :-)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Thanks a lot. I got it.

daudiam
Junior Poster
152 posts since Dec 2009
Reputation Points: 2
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: