Java Reference
In-Depth Information
< Day Day Up >
Puzzle 74: Identity Crisis
This program is incomplete. It lacks a declaration for Enigma , a class that extends
java.lang.Object . Provide a declaration for Enigma that makes the program print false :
public class Conundrum {
public static void main(String[] args) {
Enigma e = new Enigma();
System.out.println(e.equals(e));
}
}
Oh, and one more thing: You must not override equals .
Solution 74: Identity Crisis
At first glance, this may seem impossible. The Object.equals method tests for object identity, and
the object passed to equals by Enigma is certainly the same as itself. If you can't override
Object.equals , the main method must print TRue , right?
Not so fast, cowboy. Although the puzzle forbids you to override Object.equals , you are permitted
to overload it, which leads to the following solution:
final class Enigma {
// Don't do this!
public boolean equals( Enigma other) {
return false;
}
}
 
 
Search WWH ::




Custom Search