Java – Method Overloading

Java Method Overloading

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.

Advantage of Method Overloading

Method overloading improves the code readability and reduces code redundancy. Method overloading also helps to achieve compile-time polymorphism.

Example of Method Overloading

If you observe the following example, Here we have created a class named Tester this class has two methods with same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).

classCalculator{publicstaticintadd(int a,int b){return a + b;}publicstaticintadd(int a,int b,int c){return a + b + c;}}

When you invoke the add() method based on the parameters you pass respective method body gets executed.

int result =Calculator.add(1,2);// returns 3;
result =Calculator.add(1,2,3);// returns 6;

Different Ways of Java Method Overloading

Method overloading can be achieved using following ways while having same name methods in a class.

  • Use different number of arguments
  • Use different type of arguments

Invalid Ways of Java Method Overloading

Method overloading cannot be achieved using following ways while having same name methods in a class. Compiler will complain of duplicate method presence.

  • Using different return type
  • Using static and non-static methods

Method Overloading: Different Number of Arguments

You can implement method overloading based on the different number of arguments.

Example: Different Number of Arguments (Static Methods)

In this example, we’ve created a Calculator class having two static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods and printing the result. Based on the type of arguments passed, compiler decides the method to be called and result is printed accordingly.

packagecom.tutorialspoint;classCalculator{publicstaticintadd(int a,int b){return a + b;}publicstaticintadd(int a,int b,int c){return a + b + c;}}publicclassTester{publicstaticvoidmain(String args[]){System.out.println(Calculator.add(20,40));System.out.println(Calculator.add(40,50,60));}}

Output

60
150

Example: Different Number of Arguments (Non Static Methods)

In this example, we’ve created a Calculator class having two non-static methods with same name but different arguments to add two and three int values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the number of arguments passed, compiler decides the method to be called and result is printed accordingly.

packagecom.tutorialspoint;classCalculator{publicintadd(int a,int b){return a + b;}publicintadd(int a,int b,int c){return a + b + c;}}publicclassTester{publicstaticvoidmain(String args[]){Calculator calculator =newCalculator();System.out.println(calculator.add(20,40));System.out.println(calculator.add(40,50,60));}}

Output

60
150

Method Overloading: Different Type of Arguments

You can implement method overloading based on the different type of arguments.

Example: Different Type of Arguments

In this example, we’ve created a Calculator class having two non-static methods with same name but different types of arguments to add two int values and two double values respectively. In main() method, we’re calling these methods using object of Calculator class and printing the result. Based on the type of arguments passed, compiler decides the method to be called and result is printed accordingly.

packagecom.tutorialspoint;classCalculator{publicintadd(int a,int b){return a + b;}publicdoubleadd(double a,double b){return a + b;}}publicclassTester{publicstaticvoidmain(String args[]){Calculator calculator =newCalculator();System.out.println(calculator.add(20,40));System.out.println(calculator.add(20.0,40.0));}}

Output

60
60.0

Comments

Leave a Reply

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