Skip to content

Instantly share code, notes, and snippets.

@ozkansari
Last active December 11, 2015 03:19
Show Gist options
  • Save ozkansari/4537162 to your computer and use it in GitHub Desktop.
Save ozkansari/4537162 to your computer and use it in GitHub Desktop.
Simple question about java inheritance
/**
* What will be printed to standard output?
*
* a) The code will not compile.
* b) The code compiles and "5, Super" is printed to standard output.
* c) The code compiles and "5, Sub" is printed to standard output.
* d) The code compiles and "2, Super" is printed to standard output.
* e) The code compiles and "2, Sub" is printed to standard output.
* f) The code compiles, but throws an exception.
*/
public class QuizInheritance {
class Super {
int index = 5;
public void printVal() {
System.out.print( "Super" );
}
}
class Sub extends Super {
int index = 2;
public void printVal() {
System.out.print( "Sub" );
}
}
public void test() {
Super sup = new Sub();
System.out.print( sup.index + ", " );
sup.printVal();
}
public static void main( String argv[] ) {
(new QuizInheritance()).test();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment