CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Interfaces

Interfaces
Cloneable interface

The Cloneable interface is defined as follows:

public interface Cloneable { }
This interface is empty. An interface with an empty body is referred to as a Marker Interface. A marker interface does not contain constants or methods. It is used to denote that a class possesses certain desirable properties. A class that implements the Cloneable interface is marked cloneable, and its objects can be cloned using the clone() method defined in the Object class.

How to clone a user-defined class

  • If a user-defined class implements the Cloneable interface, you can clone the class using the clone method of the Object class.
  • The clone method in the Object class has protected access. To make it available to all classes, you can override it with a clone method that has public access.
  • The clone method returns an Object type. As a result, you may need to cast the result of this method to another type of class.
  • The clone method of the Object class throws a CloneNotSupportedException. A class that overrides this method must throw or catch this exception or it won't compile.

A Product class that implements the Cloneable interface

public class Product implements Cloneable
{
    private String code;
    private String description;
    private double price;

    // the code for the constructor and methods

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

Code that uses the clone method of the Product class

// create a new product
Product p1 = new Product();
p1.setCode("java");
p1.setDescription("Murach's Beginning Java 2");
p1.setPrice(49.50);

// clone the product
Product p2 = (Product) p1.clone();

// change a value in the cloned product
p2.setPrice(44.50);

// print the results
System.out.println(p1);
System.out.println(p2);

The resulting output

Code:        java
Description: Murach's Beginning Java 2
Price:       $49.50

Code:        java
Description: Murach's Beginning Java 2
Price:       $44.50

Cloning and mutable objects

  • A mutable object is an object that can be changed.
  • To clone an object that contains an instance variable for a mutable object, you need to override the clone method and manually clone that object.

A LineItem class that implements the Cloneable interface to clone a mutable object

public class LineItem implements Cloneable
{
    private Product product;
    private int quantity;
    private double total;

    // the code for the constructors and methods

    public Object clone() throws CloneNotSupportedException
    {
        LineItem li = (LineItem) super.clone();
        Product p = (Product) product.clone();
        li.setProduct(p);
        return li;
    }
}

Code that uses the clone method of the LineItem class

Product p1 = new Product();
p1.setCode("java");
p1.setDescription("Murach's Beginning Java 2");
p1.setPrice(49.50);

LineItem li1 = new LineItem(p1, 3);

// clone the line item
LineItem li2 = (LineItem) li1.clone();

// change values in the cloned LineItem and its Product
// object
li2.setQuantity(2);
li2.getProduct().setPrice(44.50);

// print the results
System.out.println(li1);
System.out.println(li2)

The resulting output

Code: java
Description: Murach's Beginning Java 2
Price: $49.50
Quantity: 3
Total: $148.50

Code: java
Description: Murach's Beginning Java 2
Price: $44.50
Quantity: 2
Total: $89.00
Previous | Test Cloneable | Comparable