clone() inherited/overridden(?) in Arrays from Object
Ankur Gupta
Ranch Hand
Joined: Jun 13, 2000
Posts: 66
posted
0
Following is from the JLS 10.7 Array Members The members of an array type are all of the following: The public final field length, which contains the number of components of the array (length may be positive or zero) The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions All the members inherited from class Object; the only method of Object that is not inherited is its clone method In the API it specifically mentions that java.util.Arrays inherits all the methods of Object including clone. Nowhere does it mention about the above stmt in the JLS. Any comments !!? Ankur
vivek rai
Ranch Hand
Joined: May 08, 2000
Posts: 45
posted
0
java.util.Arrays is NOT the class which defines the java language arrays. There is no predefined class in the JDK for them, a class is created for an array at the runtime, depending on the type of members ( getClass() for an array of ints would print [I.. its superclass wud be Object ) an arrays class is the same as the following class: class A implements Cloneable, java.io.Serializable { public final int length = X; public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { throw new InternalError(e.getMessage()); } } } --- so it has to implement clone() method in order to implement the Clonable interface, although it eventually calls Object's clone() . cheers, Vivek
Ankur Gupta
Ranch Hand
Joined: Jun 13, 2000
Posts: 66
posted
0
Vivek, array and Arrays !! It was confusing I guess. Well! now it is clear. Thanks! Ankur