wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Overloading" Watch "Regarding Overloading" New topic
Author

Regarding Overloading

Prasad Maddipatla
Greenhorn

Joined: Apr 20, 2007
Posts: 24
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

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
Prasad Maddipatla
Greenhorn

Joined: Apr 20, 2007
Posts: 24
Its sounds good but Dynamic method dispatch is not applicable when calling overloading methods.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Prasad Maddipatla
Greenhorn

Joined: Apr 20, 2007
Posts: 24
That clears my confusion Marc.

Thanks a lot
 
Consider Paul's rocket mass heater.
 
subject: Regarding Overloading