Created
December 19, 2016 20:50
-
-
Save oscarryz/37fab40a68fa3e5051c1fb0b8c55b389 to your computer and use it in GitHub Desktop.
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
//A.java | |
class A { | |
private int i; | |
public String toString() { return ""+ i; } | |
} | |
// B.java | |
class B extends A {} | |
// Main.java | |
class Main { | |
public static void main( String [] args ) { | |
System.out.println( new B().toString() ); | |
} | |
} | |
// Compile all the files | |
javac A.java B.java Main.java | |
// Run Main | |
java Main | |
// Outout is 0 as expected as B is using the A 'toString' definition | |
0 | |
// Change A.java | |
class A { | |
public String toString() { | |
return "Nothing here"; | |
} | |
} | |
// Recompile ONLY A.java | |
javac A.java | |
java Main | |
// B wasn't modified and yet it shows a different behaviour, this is not due to | |
// inheritance but the way Java loads the class | |
Output: Nothing here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment