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 ]
Joined: Aug 04, 2007
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).
Joined: Jun 18, 2007
Here you are not overriding any method, you are just performing inheritance. All methods are overloaded methods.
Joined: Mar 03, 2007
thanks christian combarel & dolly shah!
I expected the output: GFC200. but, the answer is compile time error.
can any one please explain this?
Thank you samura
kiran reddy aluguvelli
Joined: Aug 08, 2007
Hi, The answer to the above will be compile time error only but the compile time error occurs due to the reference m(null).
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.