| Author |
regarding overloading-Dan Chisholm's Mock Exam question
|
debasmita pattnayak
Ranch Hand
Joined: Apr 12, 2007
Posts: 94
|
|
Hi Ranchers, Can anyone explain me more clearly the following code: options: a) Prints: A,A b) Prints: A,B c) Prints: B,A d) Prints: B,B e) Prints: A,C f) Compile-time error g) Run-time error h) none of the above I marked the answer as " d". but to my surprise the answer is "f". Can somebody explain me the answer in a better way.
|
debasmita
|
 |
chandra sekhar.k
Greenhorn
Joined: Mar 21, 2006
Posts: 1
|
|
Hi debasmita, One thumb rule that you can remember is "Super class references can hold child class objects where as child class reference cannot hold superclass object". next in your question, all each and every method m() defined ,can handle m(c,c). because all are super classes of c. next "method call to the methodbody linkage will be done at the time of compile time only" so you are putting the compiler in confusion ,like which method to link to your m(c,c). so..the above situation leads to ambiguity...thats why compile time error... i hope you got it....
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
There is method call ambiguity that's why compiler error. Let's see how: Compiler has to select most specific method to satisfy your method call. Method selection should be so that others method also pass the test without any bother. Now lets check, whether m(B,B) is appropriate, (think in a way that if m(B,B) does not exist and any one method exist individually. #1- m(A a, B b)will do the job #2- m(B b,A a) will also do the job #3- m(A a,C c), will NOT pass the method call Because test #3 fails, your method selection is not most OK. In this way we see that none of the method is more appropriate than the other. How was that technique? Thanks,
|
cmbhatt
|
 |
yogesh srinivasan
Ranch Hand
Joined: Jun 08, 2007
Posts: 55
|
|
but still i'm confused on how this ambiguity is to be identified with respect to the method call
|
 |
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
|
|
Originally posted by yogesh srinivasan: but still i'm confused on how this ambiguity is to be identified with respect to the method call
Have a look at the following discussion.
|
 |
 |
|
|
subject: regarding overloading-Dan Chisholm's Mock Exam question
|
|
|