Skip to content

Instantly share code, notes, and snippets.

@patrickgombert
Last active August 29, 2015 13:56
Show Gist options
  • Save patrickgombert/9165881 to your computer and use it in GitHub Desktop.
Save patrickgombert/9165881 to your computer and use it in GitHub Desktop.
public static final class Ratio extends Number {
/* .. */
}
public static final class Addition {
public static Number add(Integer x, Integer y) {
/* .. */
}
public static Number add(Ratio x, Ratio y) {
/* .. */
}
public static Number add(Ratio x, Integer y) {
/* .. */
}
public static Number add(Number x, Number y) {
Class xClass = x.getClass();
if (xClass == Integer.class)
x = (Integer) x;
else if (xClass == Ratio.class)
x = (Ratio) x;
Class yClass = y.getClass();
if (yClass == Integer.class)
y = (Integer) y;
else if (yClass == Ratio.class)
y = (Ratio) y;
return (Number) add(x, y);
}
}
Addition.add(new Integer(..), new Integer(..)) // Is good
Addition.add(new Ratio(..), new Ratio(..)) // Is good
Addition.add(new Ratio(..), new Integer(..)) // Infinite recursion in the add(Number, Number) definition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment