Java Reference
In-Depth Information
}
Although lazy initialization fixes the problem, it is a bad idea to have one value class extend
another, adding a field that affects equals comparisons. You can't provide value-based equals
methods on both the superclass and subclass without violating the general contract for
Object.equals or eliminating the possibility of meaningful comparisons between superclass and
subclass instances [EJ Item 7].
The circular instance initialization problem is a can of worms for language designers. C++
addresses the problem by changing the type of the object from the superclass type to the subclass
type during construction. With this solution, the original program in this puzzle would print [4,2] .
We're not aware of any popular language that addresses this issue satisfactorily. Perhaps it is worth
considering making circular instance initialization illegal by throwing an unchecked exception
when a superclass constructor calls a subclass method.
To summarize, you must never call an overridable method from a constructor under any
circumstances. The resulting circularities in instance initialization can be fatal. The solution to this
problem is lazy initialization [EJ Items 13, 48].
< Day Day Up >
 
 
Search WWH ::




Custom Search