In this case b will see E.amethod, and call it. ----------- 2.
In this case b will NOT see E.amethod, and call Base.amethod.
I know that the difference is 1. is override, and 2. is overload, but I don't understand WHY it works like this. Pls. help. Thanx. DD Edited by Corey McGlone: Added CODE tags [ June 23, 2003: Message edited by: Corey McGlone ]
Well the answer lies not in overloading or overridding but in inhertance As you know in the example when u call b.<anything> the scope of the compiler is only for the methods in Base as compiler see that the parameter is int which can be promoted to double (ref JLS). It will bind b.amethod(1) call to amethod(double) base method instead of int. One important point remember since u overload the amethod(int) in subclass b.amethod() cannot see this new method at all (inhertance rule). Hope this helps.
In the first scenario, the two methods have the exact same signature. This creates an overridden method and, hence, the JVM uses dynamic binding to determine which method should be invoked at runtime. In the second scenario, the two methods have different signatures (one takes a double while the other takes an int) so we have what is known as method overloading (multiple methods with the same "name" but different signatures). In this case, the compiler can determine which method should be invoked at compile time. Be sure to check out these sections of the JLS for more info: §8.4.6 Inheritance, Overriding, and Hiding §8.4.7 Overloading I hope that helps, Corey [ June 23, 2003: Message edited by: Corey McGlone ]
Hi In case 1 , the complier knows that amethod is overridden , so leaves the decision of which method to call until runtime. In case 2 , as amethod is overloaded , the complier will try to match up the method based based on the parameters of the reference type. A match is possible ( 1 can fit in a double ). If this was not possible you'd get a compile time error. I'm not sure why it doesn't use the actual object type at run-time. Joey