/** * This example is adapted from: * * Kim B. Bruce, Luca Cardelli, Giuseppe Castagna, The Hopkins Objects Group, * Gary T. Leavens, and Benjamin Pierce. On Binary Methods, * Theory and Practice of Object Systems 1 (3), 1995, pp. 217-238. * * http://www.cs.jhu.edu/labs/pll/constraints/#6 */ class Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } public boolean equal(Point p) { return x == p.x && y == p.y; } } class ColorPoint extends Point { public String color; public ColorPoint(double x, double y, String color) { super(x, y); this.color = color; } public boolean equal(Point p) { if (p instanceof ColorPoint) { ColorPoint cp = (ColorPoint)p; return color.equals(cp.color) && x == cp.x && y == cp.y; } else return false; } } class BinaryMethod { public static void breakit(Point p) { Point nuPt = new Point(3.2, 4.5); if (p.equal(nuPt)) { // ... } } public static void main(String[] args) { breakit(new Point(0, 0)); breakit(new ColorPoint(0, 0, "blue")); } }