by Dinesh Thakur Category: Classes

When multiple methods in the same class have the same name but differ in the number or types of parameters, it is known as Method Overloading. When an overloaded method is invoked, it is the responsibility of the compiler to select the appropriate overloaded method based on the number of argument(s) passed and if the numbers of argument(s) are same then depending upon the type of argument(s) passed to the method. Thus, the key to method overloading is a method's parameter list. A method's return type is not enough to distinguish between two overloaded methods. If the compiler detects two methods declarations with the same name and parameter list but different return types then it will generate an error.

When you work with a Java class library, you often encounter classes that have numerous methods with the same name. We have already being using this aspect of the language implicitly when displaying text using a variety of println) method. For example

System.out.println("Hello Java");

System.out.println (59); //displays int

System.out.println(true); //displays boolean

System.out.println(31.37); //displays double

System.out.println(); //no parameter

Although all these methods have the same name (println) but take different parameter list. The first takes a String, the second takes a int, third takes a boolean, fourth takes a double and fifth takes no parameter at all. Thus, println ()method is overloaded.

Method overloading is useful when you want to create a collection of methods that perform closely related functions under different conditions. These conditions are typically embodied in the parameters passed to each version of the method.

In order to understand the concept of method overloading, let us consider an example of a method that is used to calculate sum of two numbers.

class SumTwoNum
     void sum(int x, int y)
        System.out.println("Sum of Two Integer Numbers : " + (x+y));
     void sum(double x,double y)
        System.out.println("Sum of Two Double Numbers : " + (x+y));
     void sum(char x,char y)
        System.out.println("Sum of Two Characters are : " + (x+y)); 
  public class MethodOverloading
     public static void main(String[] args)
         SumTwoNum obj = new SumTwoNum();
         obj.sum(10,20);   //call sum method with int parameter
         obj.sum(7.52,8.14); //call sum method with double parameters
         obj.sum('A','B'); //call sum method with char parameters

Method Overloading in Java with Example

In the above program, three methods are defined and invoked which have the same name sum () but differ only in their parameter types (int, double and char). For each call of the method, the compiler chooses the appropriate method by comparing the types of the arguments in the call against the parameter list of each defined method. So when the statement obj.sum (7.52, 8.14); is executed, the compiler invokes the method which takes double type parameters, as seen in output.

The above example shows the concept of method overloading in which multiple methods

with the same name and same number of parameters differ only in their data types. The compiler can also distinguish the multiple methods with same name by different number of parameters of the method. This can be illustrated with the help of following program.

Advantages of Method Overloading

The advantages of method overloading are:

1. We need to remember single name instead of multiple names of the methods that perform similar type of operations. This helps in reducing the complexity of making large programs.

2. Overloaded methods that perform similar tasks can make complex programs more readable, understandable and easy to debug.

3. Maintainability of the program becomes easier.

Method overloading is a powerful tool for creating group of related methods that only differ in the type of data sent to it. However, if not used properly such as using methods with same name for different purposes can cause considerable confusion which leads to additional overhead in terms of maintainability.
For Example: Methods performing various arithmetic operations like addition, subtraction, multiplication, division should not be overloaded with same named functions as they perform different unrelated operations ..
If the methods differ only in the return type then they are not considered as overloaded methods and hence the compiler generates an error message. This is because it is not possible for the compiler to determine which version of the method to call.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles