Last active
December 11, 2015 03:19
-
-
Save ozkansari/4537162 to your computer and use it in GitHub Desktop.
Simple question about java inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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