Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

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).

class Calculator{
   public static int add(int a, int b){
      return a + b;
   }
   public static int add(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.

package com.tutorialspoint;

class Calculator{
   public static int add(int a, int b){
      return a + b;
   }
   public static int add(int a, int b, int c){
      return a + b + c;
   }
}

public class Tester {
   public static void main(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.

package com.tutorialspoint;

class Calculator{
   public int add(int a, int b){
      return a + b;
   }
   public int add(int a, int b, int c){
      return a + b + c;
   }
}

public class Tester {
   public static void main(String args[]){
      Calculator calculator = new Calculator();   
      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.

package com.tutorialspoint;

class Calculator{
   public int add(int a, int b){
      return a + b;
   }
   public double add(double a, double b){
      return a + b;
   }
}

public class Tester {
   public static void main(String args[]){
      Calculator calculator = new Calculator();   
      System.out.println(calculator.add(20, 40));
      System.out.println(calculator.add(20.0, 40.0));
   }
}

Output

60
60.0
Advertisements