As per Josh Bloch's Effective java :-
do not use the clone method to make a defensive copy of a parameter whose type is subclassable by untrusted parties.
Now taking a expert from his book only:-
public final class Period {
private final Date start;
private final Date end;
/**
* @param start the beginning of the period
* @param end the end of the period; must not precede start
* @throws IllegalArgumentException if start is after end
* @throws NullPointerException if start or end is null
*/
public Period(Date start, Date end) {
if (start.compareTo(end) > 0)
throw new IllegalArgumentException(
start + " after " + end);
this.start = start;
this.end = end;
}
public Date start() {
return start;
}
public Date end() {
return end;
}
... // Remainder omitted
}
I do not get what wrong would happen if I modify a accesor method to return a copy of date object using clone function instead of copy using constructor like this :-
public Date start() {
return start.clone();
}
instead of
public Date start() {
return new Date(start.getTime());
}
How possibly can a instance of malicious subclass be returned?