If DisplayMode.equals is called with an Object (versus and actual
DisplayMode) and if that object is actually a DisplayMode, then the
following code will cause infinite recursion:
if (dm instanceof DisplayMode) {
return equals(dm);
}
The following code demonstrates the problem; when we perform the first equals()
test on a generic Object (dm0.equals(obj0)), the application gets into an infinite loop.
import java.awt.*;
public class DMTest {
public static void main(String args[]) {
DisplayMode dm0 = new DisplayMode(1024, 768, 32, 60);
DisplayMode dm1 = new DisplayMode(1024, 768, 32, 60);
DisplayMode dm2 = new DisplayMode(1024, 768, 32, 75);
Object obj0 = (Object)dm0;
Object obj1 = (Object)dm1;
Object obj2 = (Object)dm2;
System.out.println("dm0.equals(dm1) = " + dm0.equals(dm1));
System.out.println("dm1.equals(dm0) = " + dm1.equals(dm0));
System.out.println("dm0.equals(dm2) = " + dm0.equals(dm2));
System.out.println("dm2.equals(dm0) = " + dm2.equals(dm0));
System.out.println("dm1.equals(dm2) = " + dm1.equals(dm2));
System.out.println("dm2.equals(dm1) = " + dm2.equals(dm1));
System.out.println("dm0.equals(obj0) = " + dm0.equals(obj0));
System.out.println("dm0.equals(obj1) = " + dm0.equals(obj1));
System.out.println("dm0.equals(obj2) = " + dm0.equals(obj2));
System.out.println("dm1.equals(obj0) = " + dm1.equals(obj0));
System.out.println("dm1.equals(obj1) = " + dm1.equals(obj1));
System.out.println("dm1.equals(obj2) = " + dm1.equals(obj2));
System.out.println("dm2.equals(obj0) = " + dm2.equals(obj0));
System.out.println("dm2.equals(obj1) = " + dm2.equals(obj1));
System.out.println("dm2.equals(obj2) = " + dm2.equals(obj2));
System.out.println("Passed");
}
}