How to clone a Java object with the clone() method
I have a question regarding properly implementing the clone()
method for a class in java.
I know that this is bad practice, but I need to know this for an exam..
In the above discussion they say to call super.clone()
- but I don't udnerstand what happens if the super function doesn't implement Clonable.
For example, say I have a class X that extends Y. X implements Clonable and Y doesnl't. Y's clone()
method should throw an Exception. Then what do we do in this case?
All the explanations I could find somehow assume that all superclasses implement Clonable, or at least that's what I understood..
EDIT:
Check out this code please:
public class Employee implements Cloneable {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Object clone()throws CloneNotSupportedException{
return (Employee)super.clone();
}
public static void main(String[] args) {
Employee emp = new Employee("Abhi");
try {
Employee emp2 = (Employee) emp.clone();
System.out.println(emp2.getName());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
It is taken from here: https://www.javacodegeeks.com/2018/03/understanding-cloneable-interface-in-java.html
Similar code can be found in many tutorials.
Why can they use super.clone()
when the superclass (which in this case is Object
) does not implement Clonable - that would result in an Exception.