• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overloaded methods taking object arguments

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what determines which method is called?first I expected the method taking a B object will be called since it is the closest superclass in the inheritence tree but the result was
the opposite.
what are the rules governing which method will be called in occasions like this?
thanks
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Kevin, please QuoteYourSources for the code snippet above.

Rules are JVM will choose the closest class in the hierarchy. Why do you think that the version with method argument as B will
be called? If you look at the invocations which are


Here c1 is of type C and C extends A, so the closest class in the hierarchy is class A, so the version taking n A reference will be invoked.

Now line 12, we say the type which is passed is D and D extends C and C extends A, so the closest hierarchy again is A, so again the method taking
reference A will be called.

B doesn't come in hierarchy for the invocations because neither C or D extends from B, but rather directly or indirectly from A.

Hope this helps,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even, if you try to amend it something like this,


Changed the argument to Object rather then A, still version with Object reference will be called. It is because still B has no direct or
indirect relation with class C or D.

Hope this helps,
 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
my bad.I had read it as if it was C & D were extended from B.
and about the source, I dont have a source to post since I found it on a hand out given by a tutor.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

You are welcome mate.

Happy Preparation,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duplicate message. Sorry

 
kevinn lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Duplicate message. Sorry



thanks once again for the reply
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic