• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Overloading

 
Prasad Maddipatla
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its sounds good but Dynamic method dispatch is not applicable when calling overloading methods.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clears my confusion Marc.

Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic