Skip to content

Instantly share code, notes, and snippets.

@karadaisy
Last active December 14, 2015 19:39
Show Gist options
  • Save karadaisy/5137971 to your computer and use it in GitHub Desktop.
Save karadaisy/5137971 to your computer and use it in GitHub Desktop.
public class DynamicDispatch {
interface GeometricValue {
void intersects(GeometricValue v);
}
static class Point implements GeometricValue {
@Override
public void intersects(GeometricValue v) {
throw new UnsupportedOperationException();
}
public void intersects(Point p) {
System.out.println("Testing intersection with Point");
}
public void intersects(Line l) {
System.out.println("Testing intersection with Line");
}
}
static class Line implements GeometricValue {
@Override
public void intersects(GeometricValue v) {
throw new UnsupportedOperationException();
}
}
public static void main(String args[]) {
new Point().intersects(new Line()); // => Testing intersection with Line
GeometricValue p = new Point();
GeometricValue l = new Line();
p.intersects(l); // => throws UnsupportedOperationException
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment