2

clone() method is not visible by default in Object class so how does it does not give error for Array types?
Does this means that there is a type called int[] of which implementation is written in java and if yes where to find it ?
and if it is written then why not write it completely?
I mean why not implement every method properly not just the behaviour from Object Class.

    int[] a ={1,2,3};
    Object object = new Object();
    object.clone();//Does not compile since clone is protected.
    a.clone();// allowed as this method is from int[] 
Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • [JLS](http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html) - All methods of class Object may be invoked on an array and [SO Thread](http://stackoverflow.com/questions/2960574/do-we-inherit-from-object) – KV Prajapati Aug 23 '12 at 06:50
  • @AVD `clone` is `protected` in Object. `clone` is public in arrays. You did not answer that. – obataku Aug 23 '12 at 06:53

5 Answers5

6

All arrays implement the Cloneable interface.

See the relevant part of the Java Language Specification: §10.7 Array Members

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[]. A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

obataku
  • 29,212
  • 3
  • 44
  • 57
Transcendence
  • 2,616
  • 3
  • 21
  • 31
  • 1
    I also wanted to know where is that implementation? And if Java has implemented `clone()` method for `Arrays` why not implement `equals()` method which will check of elements like `ArrayList`. – Amit Deshpande Aug 23 '12 at 09:31
  • You probably can find the implementation in the source code. equals() is implemented for Arrays as part of the Arrays class methods http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#equals%28boolean[],%20boolean[]%29 – Transcendence Aug 23 '12 at 17:58
  • still weird that they decided to override clone() but not equals(), no? – peter Nov 06 '22 at 17:31
0

It's because there's a Cloneable interface that makes it so the Object.clone() method is accessible. Arrays in Java already implement Cloneable, so you can clone an array object.

An arbitrary Object on the otherhand, cannot guarantee that it can make a field-for-field copy of itself.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

Because arrays cannot be extended as normal objects. The only way to make an array Cloneable was to implement Cloneable interface from the start. For the same reason an array implements by default Serializable.

dcernahoschi
  • 14,968
  • 5
  • 37
  • 59
0

An array is implemented as an instance of a dynamically created class by the JVM. That class has a special name and it implements the Cloneable and Serializable interfaces as noted by other posters above. (It also has a public final field named length...).

That class also has a concrete and public implementation of the protected Object.clone() method. Java allows the access level of a method (public, private, protected & the default package-protected access levels) to be made less restrictive when that method is overridden. In other words, a protected method can be made public in a subclass and that is what the JVM does with the protected clone() method for the array class implementation.

Vikash Madhow
  • 1,287
  • 11
  • 15
-1

Its because clone() is protected in Object class and protected access specifier says you can access the superclass protected method in the subclass of the same package. By this reasoning, Object class is in java.lang package and you are accessing it from a different package and that's why you are getting compilation error.

Georgios
  • 4,764
  • 35
  • 48
MSRao
  • 1