Home > Java > Method overloading in Java – Part 2

Method overloading in Java – Part 2

Method overloading with type promotion:
One type is promoted to another implicitly if no matching datatype is found. Let’s understand the concept by the figure given below:

 Img02

Example: Method Overloading with Type Promotion

If there are matching type arguments in the method, type promotion is not performed:

Example: Method Overloading with Type Promotion if matching found

Example: Method Overloading with Type Promotion if matching found

 

If there are no matching type arguments in the method, and each method promotes similar number of arguments, there will be ambiguity:

Example:

Why Method Overloading is not possible by changing the return type of method?

In java, method overloading is not possible by changing the return type of the method because there may occur ambiguity. Let’s see how ambiguity may occur in the below example.

 

This Article is TAGGED in , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">