Skip to content

Instantly share code, notes, and snippets.

@benjholla
Created October 1, 2018 16:23
Show Gist options
  • Save benjholla/42633cb2c4f396b7e1e871d04254d236 to your computer and use it in GitHub Desktop.
Save benjholla/42633cb2c4f396b7e1e871d04254d236 to your computer and use it in GitHub Desktop.
Java Puzzle 13 (spot the bug if one exists)
public class Puzzle13 {
public static void main(String[] args){
A b1 = new B();
A c1 = new C();
A b2 = b1;
A c2 = c1;
// what will get printed?
b2.print(c2);
}
public static class A extends Object {
public void print(A object) {
System.out.println("An instance of " + object.getClass().getSimpleName()
+ " was passed to A's print(A object)");
}
}
public static class B extends A {
public void print(A object) {
System.out.println("An instance of " + object.getClass().getSimpleName()
+ " was passed to B's print(A object)");
}
}
public static class C extends B {
public void print(A object) {
System.out.println("An instance of " + object.getClass().getSimpleName()
+ " was passed to C's print(A object)");
}
}
public static class D extends A {
public void print(A object) {
System.out.println("An instance of " + object.getClass().getSimpleName()
+ " was passed to D's print(A object)");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment