Created
February 28, 2014 19:46
-
-
Save vaskoz/9278389 to your computer and use it in GitHub Desktop.
Early vs Late Binding in Java
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
class A { | |
void print() { | |
System.out.println(secret()); | |
} | |
String secret() { | |
return "A"; | |
} | |
static void staticPrint() { | |
System.out.println(staticSecret()); | |
} | |
static String staticSecret() { | |
return "static A"; | |
} | |
} | |
class B extends A { | |
@Override String secret() { | |
return "B"; | |
} | |
static String staticSecret() { | |
return "static B"; | |
} | |
} | |
public class EarlyLateBindings { | |
public static void main(String[] args) { | |
A a = new B(); | |
a.print(); // Late binding for all methods (virtual) | |
a.staticPrint(); // Early binding for Java statics | |
B b = (B)a; | |
b.staticPrint(); // Early binding for Java statics | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment