does the above code uses overloading & overrridding ?
hierachy of inheritance:
A void m1(A a) ^ B void m1(A a) ^ void m1(B b)
C void m1(A a) ^ void m1(B b) void m1(C c)
D void m1(A a) //* void m1(B b) void m1(C c) void m1(D d)
when I say d1.m1(a1); at run time D's m1 method is called(which is overriding). but, it takes an argument of type a1. so, the inherited method with the parameter m1(A a) from D * is called( which is overloading).
so, it does overridding & overloading. am i right?
correct me, If my understanding is wrong..
Thanks in Advance regards samura.
[ August 21, 2007: Message edited by: samura babu ] [ August 21, 2007: Message edited by: samura babu ]
Hi, You can't both override AND overload a method. Overload : You must change the parameters list of the method (numbers of parameters and/or type). The method to use is chosen at compile time according to the arguments type. It has nothing to do with polymorphism.
Override : The parameters list must exactly match that of the method from the superclass. However,the return type can be subtype of the return type declared for the method from the superclass. The method to call is decided at runtime according to the object type (and not the reference type).
All the three versions of method m() can accept null value. Now when more than one methods are candidate for calling, the compiler looks for the most specific parameter type for the passed value to determine which method to call. But in this example, compiler found that the method m() with both String and GFC200 are most specific because they both are extending Object, so compiler cannot determine which one to call results an ambiguous method call error.
Hope this helps.
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop