Cloning by implementing Clonable interface

suggest change

Cloning an object by implementing the Cloneable interface.

public class Sheep implements Cloneable {

    private String name;

    private int weight;

    public Sheep(String name, int weight) { = name;
        this.weight = weight;

    public Object clone() throws CloneNotSupportedException {
        return super.clone();


// create a sheep
Sheep sheep = new Sheep("Dolly", 20);
// clone the sheep
Sheep dolly =  (Sheep) sheep.clone(); // is "Dolly" and dolly.weight is 20

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents