19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.18 Which of the following is the correct method header for the <strong>com</strong>pareTo method in<br />

the String class?<br />

public int <strong>com</strong>pareTo(String o)<br />

public int <strong>com</strong>pareTo(Object o)<br />

15.19 Can the following code be <strong>com</strong>piled? Why?<br />

Integer n1 = new Integer(3);<br />

Object n2 = new Integer(4);<br />

System.out.println(n1.<strong>com</strong>pareTo(n2));<br />

15.20 You can define the <strong>com</strong>pareTo method in a class without implementing the<br />

Comparable interface. What are the benefits of implementing the Comparable<br />

interface?<br />

15.21 True or false? If a class implements Comparable, the object of the class can invoke<br />

the <strong>com</strong>pareTo method.<br />

15.7 The Cloneable Interface 577<br />

15.7 The Cloneable Interface<br />

The Cloneable interface defines the <strong>com</strong>pareTo method for <strong>com</strong>paring objects.<br />

Often it is desirable <strong>to</strong> create a copy of an object. To do this, you need <strong>to</strong> use the clone<br />

method and understand the Cloneable interface.<br />

An interface contains constants and abstract methods, but the Cloneable interface is a<br />

special case. The Cloneable interface in the java.lang package is defined as follows:<br />

package java.lang;<br />

Key<br />

Point<br />

java.lang.Cloneable<br />

public interface Cloneable {<br />

}<br />

This interface is empty. An interface with an empty body is referred <strong>to</strong> as a marker interface.<br />

A marker interface does not contain constants or methods. It is used <strong>to</strong> denote that a class possesses<br />

certain desirable properties. A class that implements the Cloneable interface is<br />

marked cloneable, and its objects can be cloned using the clone() method defined in the<br />

Object class.<br />

Many classes in the <strong>Java</strong> library (e.g., Date, Calendar, and ArrayList) implement<br />

Cloneable. Thus, the instances of these classes can be cloned. For example, the following code<br />

marker interface<br />

1 Calendar calendar = new GregorianCalendar(2013, 2, 1);<br />

2 Calendar calendar1 = calendar;<br />

3 Calendar calendar2 = (Calendar)calendar.clone();<br />

4 System.out.println("calendar == calendar1 is " +<br />

5 (calendar == calendar1));<br />

6 System.out.println("calendar == calendar2 is " +<br />

7 (calendar == calendar2));<br />

8 System.out.println("calendar.equals(calendar2) is " +<br />

9 calendar.equals(calendar2));<br />

displays<br />

calendar == calendar1 is true<br />

calendar == calendar2 is false<br />

calendar.equals(calendar2) is true<br />

In the preceding code, line 2 copies the reference of calendar <strong>to</strong> calendar1, so calendar<br />

and calendar1 point <strong>to</strong> the same Calendar object. Line 3 creates a new object that is the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!