OverrideTest.java
Download OverrideTest.java
1: // Code fragments to show when the method invoked is
2: // over-loaded, instead of overrided.
3: //
4: // written 10/2008 by Wayne Pollock, Tampa Florida USA
5:
6: class Base
7: {
8: public void foo ( int x )
9: {
10: System.out.println( "foo(int)" );
11: }
12: }
13:
14: class Derived extends Base
15: {
16: public void foo ( long x )
17: {
18: System.out.println( "foo(long)" );
19: }
20: }
21:
22: public class OverrideTest
23: {
24: public static void main ( String [] args )
25: {
26: Base b = new Derived();
27: Derived d = (Derived) b;
28:
29: // b.foo( 0L ); // won't compile!
30: b.foo( 0 ); // Invokes Base.foo
31: ((Derived)b).foo( 0L ); // Invokes Derived.foo
32: ((Derived)b).foo( 0 ); // Invokes Base.foo
33: d.foo( 0L ); // Invokes Derived.foo
34: d.foo( 0 ); // Invokes Base.foo
35: }
36: }
37:
38: // Experiment with this: try over-riding instead, but change
39: // Derived.foo to "public"