• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

regarding overloading-Dan Chisholm's Mock Exam question

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but still i'm confused on how this ambiguity is to be identified with respect to the method call
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic