I don\'t understand the mechanism of cloning custom object. For example:
public class Main{
public static void main(String [] args) {
Person pe
There's no need to create the object explicitly here in clone()
method. Just by calling super.clone()
will create the copy of this object. It will perform the shallow clone.
It's the same purpose as any such interface. Primarily it allows methods (etc.) to accept ANY Clonable object and have access to the method they need without limiting themselves to one specific object. Admittedly Clonable is probably one of the less useful interfaces in this respect, but there's certainly places where you might want it. If you want more of an idea consider the interface Comparable which for example allows you to have sorted lists (because the list doesn't need to know what the object is, only that they can be compared).