Skip to content

Instantly share code, notes, and snippets.

@george-hawkins
Last active January 18, 2018 12:29
Show Gist options
  • Save george-hawkins/a7fee06197a566beb30482f44522ca2d to your computer and use it in GitHub Desktop.
Save george-hawkins/a7fee06197a566beb30482f44522ca2d to your computer and use it in GitHub Desktop.
// Compare IGroupable (with a type parameter) here with IGroupableX below (without a type argument).
// In what situation will the generics version buy you something that the non-generic version won't.
interface IGroupable<E extends IGroupable<E>> {
E fraction(double fraction);
public static class AlphaGroupable implements IGroupable<AlphaGroupable> {
@Override
public AlphaGroupable fraction(double fraction) { return null; }
}
public static class BetaGroupable implements IGroupable<BetaGroupable> {
@Override
public BetaGroupable fraction(double fraction) { return null; }
}
// Fraction returns an IGroupable that is not the containing class.
public static class GammaGroupable implements IGroupable<BetaGroupable> {
@Override
public BetaGroupable fraction(double fraction) { return null; }
}
}
interface IGroupableX {
IGroupableX fraction(double fraction);
public static class AlphaGroupable implements IGroupableX {
@Override
public AlphaGroupable fraction(double fraction) { return null; }
}
public static class BetaGroupable implements IGroupableX {
@Override
public BetaGroupable fraction(double fraction) { return null; }
}
// Fraction returns an IGroupable that is not the containing class.
public static class GammaGroupable implements IGroupableX {
@Override
public BetaGroupable fraction(double fraction) { return null; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment