Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Regarding Overloading

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In overriden version of method calls is dicide at runtime based on object type which means it is "Dynamic method dispatch" but in overloading version of method calls it depends on the reference type not the object type at compile time. Why dynamic method dispatch wont work in overloading version of method calls.

so can any body help us to solve this confusion.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear: you can override an overloaded method, and the JVM will do dynamic dispatch on those overridden versions of the method. Perhaps that's your source of confusion.
 
Prasad Maddipatla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its sounds good but Dynamic method dispatch is not applicable when calling overloading methods.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasad, the difference is that when you are calling an overridden method, it's being called on the polymorphic object - but when you are calling an overloaded method, you are passing a reference.

Consider the following code which shows an example of each:

In Example 1 the action uses an object, not a reference. The object is a B and knows that it is a B.

Example 2 uses a reference. The reference has absolutely no clue what the actual object type is - the reference can only say "I know that my object implements or extends my type, so I can assure you that the API of my reference type will be valid for this object... whatever it is." It's because of this that the overloaded method must choose to pick the overloaded version that matches the reference type.

Could Java have been implemented to allow the polymorphic overloading? Probably. I would guess the performance hit of having to grab the object and compare its actual class to all the possible overloaded possibilities wouldn't be worth it. Besides, it's rare that such a feature would really be valuable.
 
Prasad Maddipatla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That clears my confusion Marc.

Thanks a lot
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic