If clone()
is a part of the Object
class , then why do we need to implement Clonable
interface to use clone()
?
I have read that clone()
is protected member of Object
, then what is the relationship between clone()
and Clonable
interface.
Sorry if I sound stupid. I have just began learning Java.
Cloneable
is a marker interface. It doesn't have any methods. Just to whitelist your class to make Cloneable
From docs
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.