Created
February 25, 2014 03:10
-
-
Save VolatileDream/9201901 to your computer and use it in GitHub Desktop.
A quick test to show behaviour of static function hiding 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
public class StaticTest { | |
static class A { | |
static void method(){ | |
System.out.println("A:method()"); | |
} | |
} | |
static class B extends A{ | |
static void method(){ | |
System.out.println("B:method()"); | |
} | |
} | |
public static void main(String[] args) { | |
// test1 | |
test1(new A()); | |
test1(new B()); | |
// test2(new A()); | |
test2(new B()); | |
// these are always predictable. | |
A.method(); | |
B.method(); | |
} | |
// Here is a nasty way to get yourself into a lot | |
// of trouble down the line. Imagine what would | |
// happen if you invoked a function this way, | |
// and some time in the future someone changed | |
// the type signature of your function from A to B. | |
public static void test1(A a){ | |
a.method(); // always outputs "A:method()" | |
} | |
public static void test2(B b){ | |
b.method(); // always outputs "B:method()" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment