Java Reference
In-Depth Information
return id;
}
public boolean equals(Object o) {
return (o instanceof Sub) && (id == ((Sub)o).id);
}
}
Solution 91: Serial Killer
Save for the fact that the program uses serialization, it looks simple. The subclass Sub overrides
hashCode and equals . The overriding methods satisfy the relevant general contracts [EJ Items 7, 8].
The Sub constructor establishes the class invariant, and does so without invoking an overridable
method ( Puzzle 51 ). The Super class has a single field, of type Set<Super> , and the Sub class adds
another field, of type int . Neither Super nor Sub requires a custom serialized form. What could
possibly go wrong?
Plenty. As of release 5.0, running the program produces this stack trace:
Exception in thread "main" AssertionError
at Sub.checkInvariant(SerialKiller.java:41)
at SerialKiller.main(SerialKiller.java:10)
 
 
Search WWH ::




Custom Search