cop 3503 fall 2012 shayan javed lecture 7
DESCRIPTION
COP 3503 FALL 2012 Shayan Javed Lecture 7. Programming Fundamentals using Java. More With Interfaces. Interfaces. Language construct for specifying functionality without implementation Method specifications but no implementations. Interfaces in Software Engineering. - PowerPoint PPT PresentationTRANSCRIPT
1 / 162
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 7
Programming Fundamentals using Java
1
2 / 162
More With Interfaces
3 / 162
Interfaces
Language construct for specifying functionality without implementation
Method specifications but no implementations.
4 / 162
Interfaces in Software Engineering
In large projects you have a lot of classes interacting with each other
5 / 162
Interfaces in Software Engineering
In large projects you have a lot of classes interacting with each other
Need formal descriptions of what a class does, and how it interacts with other classes.
6 / 162
Interfaces in Software Engineering
In large projects you have a lot of classes interacting with each other
Need formal descriptions of what a class does, and how it interacts with other classes.
Interfaces are a way to define expected behavior.
7 / 162
Interfaces in Software Engineering
In large projects you have a lot of classes interacting with each other
Need formal descriptions of what a class does, and how it interacts with other classes.
Interfaces are a way to define expected behavior.
Useful when multiple teams working in tandem.
8 / 162
Interfaces
Some interfaces already defined in Java Widely used
Comparable
Comparator
Cloneable
9 / 162
The Comparable interface
Defined in the java.lang Package
10 / 162
The Comparable interface
Defined in the java.lang Package
public interface Comparable {public int compareTo(Object o);
}
11 / 162
The Comparable interface
public interface Comparable {public int compareTo(Object o);
}
12 / 162
The Comparable interface
public interface Comparable {public int compareTo(Object o);
}
Comparisons between objects of the same type
13 / 162
The Comparable interface
public interface Comparable {public int compareTo(Object o);
}
Comparisons between objects of the same type Object1.compareTo(Object2)
14 / 162
The Comparable interface
public interface Comparable {public int compareTo(Object o);
}
Comparisons between objects of the same type Object1.compareTo(Object2) Return values:
< 0 = Object 1 < Object 2 (usually -1)
15 / 162
The Comparable interface
public interface Comparable {public int compareTo(Object o);
}
Comparisons between objects of the same type Object1.compareTo(Object2) Return values:
< 0 = Object 1 < Object 2 (usually -1) == 0 = Object 1 == Object 2
16 / 162
The Comparable interface
public interface Comparable {public int compareTo(Object o);
}
Comparisons between objects of the same type Object1.compareTo(Object2) Return values:
< 0 = Object 1 < Object 2 (usually -1) == 0 = Object 1 == Object 2 > 0 = Object 1 > Object 2 (usually 1)
17 / 162
The Comparable interface
Used commonly.
18 / 162
The Comparable interface
Used commonly.
For ex. the String and Date classes.
19 / 162
The Comparable interface
public String implements Comparable,… {public int compareTo(Object o) {
// compares the two Strings lexicographically
}}
20 / 162
The Comparable interface
public String implements Comparable,… {public int compareTo(Object o) {
// compares the two Strings lexicographically
}}
Ex.: "computer".compareTo ("comparison")
21 / 162
The Comparable interface
public String implements Comparable,… {public int compareTo(Object o) {
// compares the two Strings lexicographically
}}
Ex.: "computer".compareTo ("comparison")
Returns 20
22 / 162
The Comparable interface
"computer".compareTo ("comparison")
Returns 20
Provides the first non-zero difference in ASCII values.
23 / 162
The Comparable interface
"computer".compareTo ("comparison")
Returns 20
Provides the first non-zero difference in ASCII values. “c”, “o”, “m”, “p” are all equal
24 / 162
The Comparable interface
"computer".compareTo ("comparison")
Returns 20
Provides the first non-zero difference in ASCII values. “c”, “o”, “m”, “p” are all equal
Returns: (int)‘u’ - (int)‘a’
25 / 162
The Comparable interface
"computer".compareTo ("comparison")
Returns 20
Provides the first non-zero difference in ASCII values. “c”, “o”, “m”, “p” are all equal
Returns: (int)‘u’ - (int)‘a’
So “computer” > “comparison”
26 / 162
The Comparable interface
Implement it for the Rectangle class
27 / 162
The Comparable interface
Implement it for the Rectangle class Comparisons based on area
28 / 162
The Comparable interface
Implement it for the Rectangle class Comparisons based on area
public class Rectangle extends GeometricObject implements Comparable {public int compareTo(Object ob) {
Rectangle r = (Rectangle)ob;if (this.getArea() > r.getArea())
return 1;else if (r.getArea() > this.getArea())
return -1;else
return 0;}
}
29 / 162
The Comparable interface
What if a non-Rectangle object is passed in?
30 / 162
The Comparable interface
What if a non-Rectangle object is passed in?
A ClassCastException is thrown
31 / 162
The Comparable interface
What if a non-Rectangle object is passed in?
A ClassCastException is thrown
Will look at it later
32 / 162
The Comparable interface
Can also use Comparable as a data type
33 / 162
The Comparable interface
Can also use Comparable as a data type
String s = “aString”;(s instanceof Comparable) // returns true!
34 / 162
The Comparable interface
Can also use Comparable as a data type
String s = “aString”;(s instanceof Comparable) // returns true!
Comparable[] compObjects = new Comparable[5];
35 / 162
The Comparable interface
So now we can compare objects
36 / 162
The Comparable interface
So now we can compare objects
Would be nice if we could sort them using this info
37 / 162
Sorting
Always need data sorted by certain requirements
38 / 162
Sorting
Always need data sorted by certain requirements
How do you sort arrays?
39 / 162
Sorting
Always need data sorted by certain requirements
How do you sort arrays?
Will look at specific sorting algorithms later on.
40 / 162
Sorting
Sorting on Amazon’s website
41 / 162
Sorting
By different categories
42 / 162
Comparing Bookspublic Book implements Comparable{
String title;int popularity;double price, avgCustomerReview;Date publicationDate;
}
43 / 162
Comparing Bookspublic Book implements Comparable{
String title;int popularity;double price, avgCustomerReview;Date publicationDate;
// comparison based on pricepublic int compareTo(Object o) {
Book b = (Book)o;if (price > b.getPrice())
return 1;else if (price < b.getPrice())
return -1;else
return 0;}
}
44 / 162
Arrays.sort()
An easy way to sort an array
45 / 162
Arrays.sort()
An easy way to sort an array Works directly with most of the primitive types –
int, char, double, long, etc.
46 / 162
Arrays.sort()
An easy way to sort an array Works directly with most of the primitive types –
int, char, double, long, etc.
int numbers[] = {4, 1, 19, 8};Arrays.sort(numbers);
47 / 162
Arrays.sort()
An easy way to sort an array Works directly with most of the primitive types –
int, char, double, long, etc.
int numbers[] = {4, 1, 19, 8};Arrays.sort(numbers);
numbers = [1, 4, 8, 19]
48 / 162
Arrays.sort()
Can also sort arrays of Objects
49 / 162
Arrays.sort()
Can also sort arrays of Objects
Arrays.sort(Object[] o)
50 / 162
Arrays.sort()
Can also sort arrays of Objects
Arrays.sort(Object[] o)
Make sure that the objects in the array: Implement Comparable
51 / 162
Arrays.sort()
Can also sort arrays of Objects
Arrays.sort(Object[] o)
Make sure that the objects in the array: Implement Comparable Are of the same type (no ClassCastException thrown)
52 / 162
Arrays.sort()
Let’s sort the Books by price
53 / 162
Arrays.sort()
Let’s sort the Books by price
Book book1 = new Book(“Game Of Thrones”, 9.99);Book book2 = new Book(“The Help”, 12.99);Book book3 = new Book(“The Road”, 8.99);Book[] books = {book1, book2, book3};
54 / 162
Arrays.sort()
Let’s sort the Books by price
Book book1 = new Book(“Game Of Thrones”, 9.99);Book book2 = new Book(“The Help”, 12.99);Book book3 = new Book(“The Road”, 8.99);Book[] books = {book1, book2, book3};
Arrays.sort(books);books = [book3, book1, book2];
55 / 162
Arrays.sort()
Let’s sort Rectangles
56 / 162
Arrays.sort()
Let’s sort Rectangles
Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};
57 / 162
Arrays.sort()
Let’s sort Rectangles
Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs);
58 / 162
Arrays.sort()
Let’s sort Rectangles
Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs);
objs = [r2, r3, r1]
59 / 162
Arrays.sort()
Now we have sorted by area (for Rectangles) and by price (for Books)
60 / 162
Arrays.sort()
Now we have sorted by area (for Rectangles) and by price (for Books)
What if we want to sort by another criteria?
61 / 162
The Comparator interface
Provides an alternative way of comparison
62 / 162
The Comparator interface
Provides an alternative way of comparison
public interface Comparator {public int compare(Object o1, Object
o2);}
// To use the interface:
import java.util.Comparator;
63 / 162
The Comparator interface
public interface Comparator {public int compare(Object o1, Object o2);
}
Comparisons between objects of the same type
64 / 162
The Comparator interface
public interface Comparator {public int compare(Object o1, Object o2);
}
Comparisons between objects of the same type Object1.compare(Object1, Object2)
65 / 162
The Comparator interface
public interface Comparator {public int compare(Object o1, Object o2);
}
Comparisons between objects of the same type Object1.compare(Object1, Object2) Object3.compare(Object1, Object2)
66 / 162
The Comparator interface
public interface Comparator {public int compare(Object o1, Object o2);
}
Comparisons between objects of the same type Object1.compare(Object1, Object2) Object3.compare(Object1, Object2) Return values:
< 0 = Object 1 < Object 2 (usually -1) == 0 = Object 1 == Object 2 > 0 = Object 1 > Object 2 (usually 1)
67 / 162
The Comparator interface
Now let’s sort books by popularity.
public Book implements Comparable{String title;int popularity;double price, avgCustomerReview;Date publicationDate;
}
68 / 162
The Comparator interfacepublic Book implements Comparable, Comparator{
// Properties..// compareTo…
// comparison based on popularitypublic int compare (Object o1, Object o2) {
}}
69 / 162
The Comparator interfacepublic Book implements Comparable, Comparator{
// Properties..// compareTo…
// comparison based on popularitypublic int compare (Object o1, Object o2) {
Book b1 = (Book)o1;
}}
70 / 162
The Comparator interfacepublic Book implements Comparable, Comparator{
// Properties..// compareTo…
// comparison based on popularitypublic int compare (Object o1, Object o2) {
Book b1 = (Book)o1;Book b2 = (Book)o2;
}}
71 / 162
The Comparator interfacepublic Book implements Comparable, Comparator{
// Properties..// compareTo…
// comparison based on popularitypublic int compare (Object o1, Object o2) {
Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getPopularity() > b2.getPopularity())
return 1;if (b1.getPopularity() < b2.getPopularity())
return -1;else
return 0;}
}
72 / 162
The Comparator interface
Sort using Comparator
73 / 162
The Comparator interface
Sort using Comparator
Arrays.sort(Object[] array, Comparator object)
object = object of a class which implements Comparator
74 / 162
The Comparator interface
Sorting Books based on popularity
75 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
76 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
Arrays.sort(books, book1);
77 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
Arrays.sort(books, book1);books = [book2, book1, book3];
78 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
Arrays.sort(books, book1); // Replace book1 with book2?
79 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
Arrays.sort(books, book2); // Replace book1 with book2?
80 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
Arrays.sort(books, book2); // Replace book1 with book2?
books = [book2, book1, book3]; // same output
81 / 162
The Comparator interface
Sorting Books based on popularity
Book book1 = new Book(“Game Of Thrones”, 9.99, 2);Book book2 = new Book(“The Help”, 12.99, 1);Book book3 = new Book(“The Road”, 8.99, 20);Book[] books = {book1, book2, book3};
Arrays.sort(books, book2); // Replace book1 with book2?
books = [book2, book1, book3]; // same outputObject passed in doesn’t matter
82 / 162
The Comparator interface
Implement it for the Rectangle class Comparisons based on width
83 / 162
The Comparator interface
public class Rectangle extends GeometricObject implements Comparator {
public int compare(Object ob1, Object ob2) {Rectangle r1 = (Rectangle)ob1;Rectangle r2 = (Rectangle)ob2;if (r1.getWidth() > r2.getWidth() )
return 1;else if (r1.getWidth() < r2.getWidth()
return -1;else
return 0;}
}
84 / 162
The Comparator interface
Rectangle(width, height)
Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};
85 / 162
The Comparator interface
Rectangle(width, height)
Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs, r1); // can also pass in r2 or r3
86 / 162
The Comparator interface
Rectangle(width, height)
Rectangle r1 = new Rectangle(5.0, 2.0); // area = 10Rectangle r2 = new Rectangle(1.0, 4.0); // area = 4Rectangle r3 = new Rectangle(3.0, 2.0); // area = 6Rectangle[] objs = {r1, r2, r3};Arrays.sort(objs, r1); // can also pass in r2 or r3
objs = [r2, r3, r1]
87 / 162
The Comparator interface
Now we have sorted by area and width (for Rectangles) and by price and popularity (for Books)
88 / 162
The Comparator interface
Now we have sorted by area and width (for Rectangles) and by price and popularity (for Books)
What if we still want to sort by another criteria?
89 / 162
The Comparator interface
Already implemented Comparator in the Rectangle/Book classes though…
90 / 162
The Comparator interface
Already implemented Comparator in the Rectangle/Book classes though…
Solution: Create a new, empty class which implements Comparable
91 / 162
The Comparator interface
Already implemented Comparator in the Rectangle/Book classes though…
Solution: Create a new, empty class which implements Comparable
Comparisons based on other criteria
92 / 162
The Comparator interface
Book comparisons based on “Avg. Customer Review” (avgCustomerReview)
93 / 162
The Comparator interface
public class ReviewComparison implements Comparator {
}
94 / 162
The Comparator interface
public class ReviewComparison implements Comparator {public int compare (Object o1, Object o2) {
Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getACR() > b2.getACR())
return 1;if (b1.getACR() < b2.getACR())
return -1;else
return 0;}
}
ACR = Average Customer Review
95 / 162
The Comparator interface
To sort based on average customer review:
96 / 162
The Comparator interface
To sort based on average customer review:
Arrays.sort(books, new ReviewComparison() )
97 / 162
The Comparator interface
So we can use Comparator for comparing objects based on different criteria.
And use Arrays.sort() based on that.
98 / 162
Comparable vs. Comparator
Use Comparable ( the compareTo() method):
99 / 162
Comparable vs. Comparator
Use Comparable ( the compareTo() method): want to compare based on default natural ordering
100 / 162
Comparable vs. Comparator
Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)
101 / 162
Comparable vs. Comparator
Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)
Use Comparator ( the compare() method):
102 / 162
Comparable vs. Comparator
Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)
Use Comparator ( the compare() method): want to compare based on different behavior from
the default
103 / 162
Comparable vs. Comparator
Use Comparable ( the compareTo() method): want to compare based on default natural ordering have control of the object (source code)
Use Comparator ( the compare() method): want to compare based on different behavior from
the default do NOT have control of the object (source code)
104 / 162
Object duplication
In large systems, often deal with objects of the same type.
105 / 162
Object duplication
In large systems, often deal with objects of the same type.
Need a way to easily create clones of objects.
106 / 162
Object duplication
Example: (Halo videogame)
107 / 162
Object duplication
Would be tedious to instantiate each one individually
108 / 162
Object duplication
Easier to clone (and then maybe modify)
109 / 162
Object duplication
Products of multiple sizes/colors
110 / 162
Object duplication
Products of multiple sizes/colors
111 / 162
Object duplication
Easier to clone (and then modify color)
112 / 162
Object duplication
Two types of copies:
113 / 162
Object duplication
Two types of copies:
1. Shallow copy
114 / 162
Object duplication
Two types of copies:
1. Shallow copy
2. Deep Copy
115 / 162
Shallow copy
Bit-wise copy of an object
116 / 162
Shallow copy
Bit-wise copy of an object
New object created with exact copy of the values.
117 / 162
Shallow copy
Bit-wise copy of an object
New object created with exact copy of the values.
Will create new primitives with same values (int, char, double, etc.)
118 / 162
Shallow copy
Bit-wise copy of an object
New object created with exact copy of the values.
Will create new primitives with same values (int, char, double, etc.)
But: Will just copy over references to objects.
119 / 162
Shallow copy
References to objects copied.
120 / 162
Shallow copy
References to objects copied.
Problem: Modifying objects in clone will also modify objects in the original!
121 / 162
Shallow copy
public Book {String title;int popularity;double price, avgCustomerReview;Date publicationDate;
}
122 / 162
Shallow copy
public Book {String title;int popularity;double price, avgCustomerReview;Date publicationDate;
}
What happens if you create a shallow copy?
123 / 162
Shallow copy
Book1double price
double avg
Date publicationDate
String title
Memory
124 / 162
Shallow copy
Book1double price
double avg
Date publicationDateShallow
copy
String title
Memory
125 / 162
Shallow copy
Book1double price
double avg
Date publicationDate
Book2
String title
double price
double avg
Shallow copy
String title
Memory
Date?
126 / 162
Shallow copy
Book1double price
double avg
Date publicationDate
Book2
String title
double price
double avg
Shallow copy
String title
Memory
127 / 162
Shallow copy
Both Books point to the same Date!
128 / 162
Shallow copy
Both Books point to the same Date!
Modifying Date object in clone will also modify Date in original.
129 / 162
Shallow copy
Both Books point to the same Date!
Modifying Date object in clone will also modify Date in original.
Solution?
130 / 162
Deep Copy
Complete copy of an object
131 / 162
Deep Copy
Complete copy of an object
References are not copied – new objects are created. (all the way down the hierarchy)
132 / 162
Deep Copy
Complete copy of an object
References are not copied – new objects are created. (all the way down the hierarchy)
Of course primitives are copied too (int, char, double, etc.)
133 / 162
Deep Copy
Book1double price
double avg
Date publicationDate
String title
Memory
134 / 162
Deep Copy
Book1double price
double avg
Date publicationDateDeep
Copy
String title
Memory
135 / 162
Deep Copy
Book1double price
double avg
Date publicationDateDeep
Copy
String title
Memory
Book2double price
double avg
Date publicationDate
String title
136 / 162
The Cloneable interface
A Marker interface
137 / 162
The Cloneable interface
A Marker interface Does not contain constants or methods Used to indicate that it’s legal to clone this object
138 / 162
The Cloneable interface
A Marker interface Does not contain constants or methods Used to indicate that it’s legal to clone this object
Defined in the java.lang.package:
package java.lang;public interface Cloneable {
}
139 / 162
The Cloneable interface
Use the .clone() method
140 / 162
The Cloneable interface
Use the .clone() method
Defined in the Object class:
protected Object clone() throws CloneNotSupportedException
141 / 162
The Cloneable interface
Java classes (like Date and Calendar) implement Cloneable
142 / 162
The Cloneable interface
Java classes (like Date and Calendar) implement Cloneable
Example:
Calendar calendar = new GregorianCalendar(2004, 2, 1);
Calendar copy = (Calendar)calendar.clone();
143 / 162
The Cloneable interface
Java classes (like Date and Calendar) implement Cloneable
Example:
Calendar calendar = new GregorianCalendar(2004, 2, 1);
Calendar copy = (Calendar)calendar.clone();
calendar == copy // ?
144 / 162
The Cloneable interface
Java classes (like Date and Calendar) implement Cloneable
Example:
Calendar calendar = new GregorianCalendar(2004, 2, 1);
Calendar copy = (Calendar)calendar.clone();
calendar == copy // ? false
145 / 162
The Cloneable interface
Java classes (like Date and Calendar) implement Cloneable
Example:
Calendar calendar = new GregorianCalendar(2004, 2, 1);
Calendar copy = (Calendar)calendar.clone();
calendar == copy // ? falsecalendar.equals(copy) // ?
146 / 162
The Cloneable interface
Java classes (like Date and Calendar) implement Cloneable
Example:
Calendar calendar = new GregorianCalendar(2004, 2, 1);
Calendar copy = (Calendar)calendar.clone();
calendar == copy // ? falsecalendar.equals(copy) // ? true
147 / 162
The Cloneable interface
Did the clone() method create a shallow or deep copy?
148 / 162
The Cloneable interface
Did the clone() method create a shallow or deep copy?
Deep – since they are distinct objects
149 / 162
The Cloneable interface
A class that implements Cloneablemust override the clone() method
150 / 162
The Cloneable interface
Let’s clone the Book class
151 / 162
The Cloneable interface
Let’s clone the Book class
152 / 162
The Cloneable interface
public Book implements Cloneable {String title;int popularity;double price, avgCustomerReview;Date publicationDate;
public Object clone() throws CloneNotSupportedException {return super.clone();
}}
153 / 162
The Cloneable interface
Will that create a shallow or deep copy?
154 / 162
The Cloneable interface
Will that create a shallow or deep copy?
Shallow – just calling super.clone() creates a shallow copy.
155 / 162
The Cloneable interface
Will that create a shallow or deep copy?
Shallow – just calling super.clone() creates a shallow copy.
Need to clone references properly to create a deep copy
156 / 162
Shallow copy
Book1double price
double avg
Date publicationDate
Book2
String title
double price
double avg
Shallow copy
String title
Memory
157 / 162
The Cloneable interface
public Book implements Cloneable {Date publicationDate;
public Object clone() throws CloneNotSupportedException {
}}
158 / 162
The Cloneable interface
public Book implements Cloneable {Date publicationDate;
public Object clone() throws CloneNotSupportedException { Book book = (Book)super.clone(); // shallow copy
first
}}
159 / 162
The Cloneable interface
public Book implements Cloneable {Date publicationDate;
public Object clone() throws CloneNotSupportedException { Book book = (Book)super.clone(); // shallow copy
first book.publicationDate =
(Date)publicationDate.clone(); // deep copy
}}
160 / 162
The Cloneable interface
public Book implements Cloneable {Date publicationDate;
public Object clone() throws CloneNotSupportedException { Book book = (Book)super.clone(); // shallow copy
first book.publicationDate =
(Date)publicationDate.clone(); // deep copy return book;
}}
161 / 162
The Cloneable interface
All mutable fields should be cloned for a deep copy
162 / 162
The Cloneable interface
All mutable fields should be cloned for a deep copy
Strings are immutable, so no need to clone them separately.