- if a class implements Cloneable, Object’s clone method returns a field-by-field copy of the object; otherwise it throws CloneNotSupportedException.
- all classes that implement Cloneableshould override clonewith a public method whose return type is the class itself.
- You can increase the scope of access modifiers of an overridden method
- Calling method clone won't call the constructor
- It's OK if using super.clone() to clone immutable object. If object is mutable, we have to manually clone its properties which are mutable (like array, set, list, mutable class, ...).
- When working with thread-safe, Object’s clonemethod is not synchronized, you may have to write a synchronized clone method that invokes super.clone().
- it’s safe to say that other interfaces should not extend it, and that classes designed for inheritance (Item 17) should not implement it. Because of its many shortcomings, some expert programmers simply choose never to override the clonemethod and never to invoke it except, perhaps, to copy arrays.
Item 12: Comparable
- override compareTo method
- sometimes, compareTo method is not consistent with equals method
- Collection, Set, Map use equals method to check for existence, while sorted collections like TreeSet, TreeMap use compareTo method.
No comments:
Post a Comment