Skill-Building Exercises


Summary

Anticipated object usage scenarios must be considered during the application design phase. All classes should be evaluated against the seven object usage scenarios. Use the Object Usage Scenario Evaluation Checklist given in table 23-1 to help evaluate your class design.

Most of the methods provided by the java.lang.Object class are meant to be overridden. These include the toString(), equals(), hashCode(), clone(), and finalize() methods.

The purpose of the toString() method is to provide a string representation for a class of objects. The contents of this string are dictated by class design requirements. It’s considered good programming practice to always override the toString() method.

The purpose of the equals() method is to perform a logical equality comparison between two objects of the same class. The equals() method implements an equivalence relation specified in the Java Platform API documentation that says the method should be reflexive, symmetric, transitive, consistent, and should return false if a non-null instance object is compared against a null reference. The behavior of the equals() method has ramifications for the behavior of other methods as well.

The purpose of the hashCode() method is to provide an integer value for an object when it is used with hashtable or hash-based data structures. The hashCode() method has a general behavior contract specified in the Java Platform API documentation and its performance is dependent upon the behavior of the equals() method. Namely, fields utilized in the equals() method to perform the logical comparison should be used in the hashCode() method to generate the object’s hash code.

The purpose of the clone() method is to create a logically equal copy of an existing object. The important thing to consider when implementing the clone() method is the undesirable effects of performing a shallow copy of complex class types.

The purpose of the finalize() method is to tie-up loose ends when an object is collected by the Java Virtual Machine (JVM) garbage collector. However, do not depend on finalize() to perform a time-sensitive operation as there are no guarantees when the finalize() method will be called.

Implement the Comparable interface when a class of objects will be utilized in a collection and will therefore be subject to a natural ordering. It is recommended that the Comparable.compareTo() method perform consistently with the equals() method.

The Comparator interface is implemented by a class of objects whose job it is to impose a total ordering upon another class of objects. It is suggested that the Comparator.compare() method’s behavior be consistent with the equals() method of the class upon which it is imposing order.

Implement the Serializable interface if you need to save objects to disk or send them over a network.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net