public class OverloadTest { double x, y; public OverloadTest(double x0, double y0) { x = x0; y = y0; } // foo-1 public void foo(double dx, double dy) { x += dx; y += dy; } // foo-2 public void foo(int dx, int dy) { x *= dx; y *= dy; } // bar-1 public void bar(Point p) { System.out.print("Point class: "); p.print(); System.out.println(); } // bar-2 public void bar(ColorPoint p) { System.out.print("ColorPoint class: "); p.print(); System.out.println(); } /* 1 */ public void print() { System.out.printf("(%g, %g)", x, y); System.out.println(); } public static void main(String[] args) { OverloadTest o = new OverloadTest(1.1, 2.2); o.foo(3.3, 4.4); // foo-1 が呼ばれる o.print(); o.foo(2, 3); // foo-2 が呼ばれる o.print(); /* 2 */ ColorPoint cp = new ColorPoint(0, 0, "red"); Point p = cp; o.bar(cp); // bar-2 が呼ばれる o.bar(p); // bar-1 が呼ばれる } }