Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created September 1, 2016 12:17
Show Gist options
  • Save tonymorris/501b469c914d0b87cc8b9ac4f22b647c to your computer and use it in GitHub Desktop.
Save tonymorris/501b469c914d0b87cc8b9ac4f22b647c to your computer and use it in GitHub Desktop.
// What is the output of this program? (1)
// a) m
// b) main
// c) compile-error
// d) runtime exception
// e) something else ______
//
// Now change the access modifier on the printS method to anything but private.
// What is the output of the program now? (2)
// a) m
// b) main
// c) compile-error
// d) runtime exception
// e) something else ______
class PrivateCtorOverride {
private final String s;
private void printS() {
System.out.println(s);
}
PrivateCtorOverride(String s) {
this.s = s;
}
public static void main(String[] args) {
new PrivateCtorOverride("main").m();
}
private void m() {
new PrivateCtorOverride("m") {
void method() {
printS();
}
}.method();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment