// Satisfy the Java filename/class rules. Ignore this class. public class Subtype {} class Point { protected int x; protected int y; public Point(int x, int y) { this.x = y; this.y = y; } public int getX() { return x; } public int getY() { return y; } } class ColorPoint extends Point { protected String color; public ColorPoint(int x, int y, String color) { super(x, y); this.color = color; } public String getColor() { return color; } } /* * For which pairs (X,Y) in {A, B, C, D} does X <: Y hold? * (Assume we change the declaration of X to be include "X extends Y".) */ interface AI { Point shift(Point p); } interface BI { ColorPoint shift(ColorPoint cp); } interface CI { Point shift(ColorPoint cp); } interface DI { ColorPoint shift(Point p); } /* * Below are sample implementations of these interfaces to help reason * about subtyping rules. */ class A implements AI { Point shift(Point p) { return new Point(p.getX() - 10, p.getY() + 10); } } class B implements BI { ColorPoint shift(ColorPoint cp) { if (cp.getColor().equals("red")) { return new ColorPoint(cp.getX(), cp.getY() + 10, "blue"); } else { return cp; } } } class C implements CI { Point shift(ColorPoint cp) { if (cp.getColor().equals("red")) { return new Point(cp.getX(), cp.getY() + 10); } else { return cp; } } } class D implements DI { ColorPoint shift(Point p) { return new ColorPoint(p.getX() - 10, p.getY(), "blue"); } } Point p = new Point(1,2); ColorPoint cp = new ColorPoint(3,4,"red"); Y shifter = new X(); // px = p or cp YReturnType py = shifter.shift(px); // Use all methods of YReturnType on py.