In java we have and interface cloneable What I want to understand is why abstract class implements that interface there is still no implementation of clone() method of interface in abstract class ?
2 Answers
The interface Cloneable is just to mark that the class supports the clone method in Object.clone().
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
You dont have to implement this method, the Object.clone do this for you, in a native code:
protected native Object clone() throws CloneNotSupportedException;
Take a look at those links: Object class source code and Cloneable Interface docs
Despite that, if you have one abstract class in the hierachy level, you dont have to implement the Cloneable in children class:
abstract class Animal implements Cloneable {
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Dog extends Animal {
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
@Override
public Dog clone() throws CloneNotSupportedException {
return (Dog)super.clone();
}
The clone still supported for Java

- 2,889
- 1
- 18
- 25
In java we have and interface cloneable What I want to understand is why abstract class implements that interface there is still no implementation of clone() method of interface in abstract class ?
Object class provides default implementation for Object
's methods whose clone()
method.
When you look Object
class, you can see that clone()
specifies a particular keyword in its signature : native
.
protected native Object clone() throws CloneNotSupportedException;
The native
keyword is applied to a method to indicate that it is implemented in JNI
(Java Native Interface).
So, the implementation exists somewhere (probably in a C function but not exclusively...) but not directly in the Java source code of the method.
Finally, you should consider the Cloneable interface
as what it stands for : a marker interface
. If you want that you object to be cloneable, implement this and if you don't care, do nothing :
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
Now, if default implementation of clone()
method (shallow copy) doesn't match with how do you want to clone your object, feel free to override clone()
in the class of this object.

- 125,838
- 23
- 214
- 215