Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
java.lang.Object, hereafter referred to simply as
Object, as a base class. Because of this, all Java classes inherit methods from
Object. Half of these methods are final and cannot be overridden. However, the other methods in
Objectcan be and are overridden, often incorrectly. This article explains why it's important to implement these methods correctly and then explains how to do so.
Object declares three versions of the
wait method, as well as the methods
getClass. These methods all are final and cannot be overridden. This article discusses the remaining methods that are not final and
that often must be overridden:
I'll discuss the
clone method first, as it provides a nice collection of subtle traps without being excessively complicated. Next, I'll consider
hashCode together. These are the most difficult to implement correctly. Wrapping up the article, I'll describe how to override the
Why is it important to implement these methods correctly? In a small application written, used, and maintained by one individual, it may not be important. However, in large applications, in applications maintained by many people and in libraries intended for use by other people, failing to implement these methods correctly can result in classes that cannot be subclassed easily and that do not work as expected.
It is, for example, possible to write the
clone method so that no child classes can be cloned. This will be a problem for users who want to extend the class with the improperly
clone method. For in-house development this mistake can result in excess debug time and rework when the problem is finally discovered.
If the class is provided as part of a class library you sell to other programmers, you may find yourself rereleasing your
library, handling excess technical support calls, and possibly losing sales as customers discover that your classes can't
Erroneous implementations of
hashcode can result in losing elements stored in hashtables. Incorrect implementation of these methods can also result in intermittent,
data-dependent bugs as behavior changes over time. Again, this can result in excess debugging and extra software releases,
technical support calls, and possibly lost sales. Implementing
toString improperly is the least damaging, but can still result in loss of time, as you must debug if the name of the object is wrong.
In short, implementing these methods incorrectly can make it difficult or impossible for other programmers to subclass and use the classes with the erroneous implementation. Less serious, but still important, implementing these methods incorrectly can result in time lost to debugging.
Two themes will reappear throughout this article. The first theme is that you must pay attention to whether your implementations of these methods will continue to be correct in child classes. If not, you should either rewrite your implementations to be correct in child classes or declare your class to be final so that there are no child classes.
java.lang.Objectcan be found at Sun's Java Web site http://java.sun.com