• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
Too many men are afraid of being fools - Henry Ford. Foolish tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic