Skip to content

Instantly share code, notes, and snippets.

@kabutz
Last active April 10, 2023 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kabutz/7ccb11a7f09392af33aa5843c8e007b6 to your computer and use it in GitHub Desktop.
Save kabutz/7ccb11a7f09392af33aa5843c8e007b6 to your computer and use it in GitHub Desktop.
/** In memory of @crazybob, who loved puzzles like this. */
public class OverridingFinalToStringPuzzle {
private static class Bull {
public final String toString() {
return "Bull";
}
}
public static String convert(Bull bull) {
return "%s".formatted(bull);
}
// Don't change anything above this line
public static void main(String... args) {
// TODO: Change this method so that the converted String becomes "Bear"
// without any command line flags (thus no deep reflection on String)
String result = convert(new Bull());
if (!result.equals("Bear"))
throw new AssertionError("Should be \"Bear\"");
System.out.println("All good!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment