Output Method Overloading

If we define a method with same name as an existing method in an class, then this concept is Method overloading. Beginners may think this as tough concept, But i found this very simple. The method overloading is way to achieve polymorphism.

Need Of Method Overloading:

here I want to give a scenario in which we have a method inside our class. Let the signature of method is int sum(int x, int y);

In this case we can add two byte, short, char or int values by passing them in this method. But what if we need to add float values or we need to concatenate strings in some fashion. Then we have to create a function with different name like double  sumdouble(double c, double d);

or String sumString(String r, String s );

Here we are compelled to give different names which are not meaningful. This reduce the readability of the code.

Readability is the main advantage of Method Overloading.

Rules for Method Overriding:

Lets see some rules for Method Overriding.

  1. All overloaded methods must have same name.
  2. In Method Overloading the signature of methods must not exactly same.
  3.  Overloaded methods must have
    • Differnt number of parameters
    • or Different types of parameters
  4. We can not overload methods using different return type. Means return type is not significant in Method Overloading.


Lets have a look at following example.

In above program compiler checks the type of arguments and numbers of argument then decide, which method to call.


Output of the above program is like this.

double–double ->Sum is 33.2
int–int ->Sum is 34
double–int ->Sum is 22.2
int–double ->Sum is 35.2
int–int–int –> 25

Output Method Overloading
Output Method Overloading

Effect of Automatic Type promotion in Method Overloading

Due to automatic type promotion Overloaded method may behave some what differently.

Figure below will illustrate the automatic type promotion of java primitive data types.

Automatic type promotion in java
Automatic type promotion in java


Have a look in the following example;

Example 2:

Inside main method of

Here the second argument will be promoted to double and first method will be called.


Char–double –> 109.0


If you try this code you will get compile time error.

Hope this article is helpful for you to understand Method Overloading in Java.


If you find any mistake and query about post please comment below.

Facebook Comments