Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Created December 19, 2016 20:50
Show Gist options
  • Save oscarryz/37fab40a68fa3e5051c1fb0b8c55b389 to your computer and use it in GitHub Desktop.
Save oscarryz/37fab40a68fa3e5051c1fb0b8c55b389 to your computer and use it in GitHub Desktop.
//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