Java Reference
In-Depth Information
Recall that to prevent intentional cheating and accidental misuse,
SortDouble is designed not to trust its extended classes. For example,
if SortDouble.values (the array being sorted) were protected instead of
private , we could eliminate the probe method because sort algorithms
normally count only comparisons and swaps. But if we eliminated it, the
programmer writing an extended class could avoid using swap to swap
data. The results would be invalid in ways that might be hard to notice.
Counting probes and declaring the array private preclude some bugs as
well as intentionally devious programming.
If a class is not designed to be extended, it often will be misused by sub-
classes. If your class will have subclasses, you should design its protec-
ted parts carefully. The end result may be to have no protected members
if extended classes need no special access. If you do not design the pro-
tected part of your class, the class should have no protected members,
making subclasses rely on its public contract.
Exercise 3.11 : Find at least one security hole in SortDouble that would
let a sorting algorithm cheat on its metrics without getting caught. Fix
the security hole. Assume that the sorting algorithm author doesn't get
to write main .
Exercise 3.12 : Write a general-purpose SortHarness class that can sort
any object type. How would you provide a way to represent ordering for
the objects in a general way, given that you cannot use < to compare
them?
 
Search WWH ::




Custom Search