Table of contents
1.
Introduction 
2.
What is Method overloading?
2.1.
By changing the data type of parameters
2.2.
By changing the number of parameters
2.3.
By changing the sequence/order of parameters
3.
Frequently Asked Questions
3.1.
What is method overriding in Java?
3.2.
What is the real-time use of method overriding?
3.3.
What is method overloading in Java?
4.
Conclusion
Last Updated: Sep 7, 2024
Medium

Method Overloading in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Java is an object-oriented class-based programming language used in Front-end and Back-end. It is used to develop mobile applications, web applications, games, GUI applications, etc. It contains classes, methods, and objects encapsulated as a unit. Java is a platform-independent programming language. It can be used anywhere independent of the systems because it is compiled into bytecodes that can be run on any Java Virtual Machine(JVM).  

Method Overloading in Java

Recommended topic -  Iteration Statements in JavaDuck Number in Java

What is Method overloading?

A program should be efficient and less complex. Suppose you need to perform any computation or operation more than once in a program with different inputs. It is difficult to write different methods multiple times with different parameters as it might confuse the other programmers. So, to avoid ambiguity, we use method overloading with the same method names and different parameters. It lets us know that the methods are the same and have the same functionality.

The methods inside Java contain the logic/ functionality to execute the program. There can be multiple methods inside a single class with any return types like int, float, void, String, etc. The technique of re-using the methods with the same name but different parameters is called Method overloading. The method's return type can be either be different or the same. 

The method overloading can be done in two ways.

  1. By changing the data type of parameters.
  2. By changing the number of parameters.
  3. By changing the sequence/order of parameters

Also see, Swap Function in Java

By changing the data type of parameters

We can overload any method by changing the data types of the parameters we send while invoking the method. Let’s consider an example with the same methods now.

class MethodOverloadingExample {

   static int addition(int x, int y) {
      return x + y;
   }

   static double addition(int x, double y) {
      return x + y;
   }

   public static void main(String[] args) {
      int a = addition(8, 8);
      double b = addition(4, 5.2);
      System.out.println("addition of integers: " + a);
      System.out.println("addition of double values: " + b);
   }
}
You can also try this code with Online Java Compiler
Run Code


We created a class MethodOverloadingExample in the above example containing the method addition with different return types. We invoked the method addition from the main method with different parameters of int and double.

Output

addition of integers: 16

addition of integers and double values: 9.2

You can also find the output of this java compiler code here.

By changing the number of parameters

We can overload any method by changing the number of parameters we send while invoking the method. Let’s learn overloading with changing the number of parameters now.

class MethodOverloadingExample {

   static int addition(int x, int y, int z) {
      return x + y + z;
   }
   
   static int addition(int x, int y) {
      return x + y;
   }

   public static void main(String[] args) {
      int a = addition(8, 2, 5);
      double b = addition(4, 5);
      System.out.println("addition of 3 integers: " + a);
      System.out.println("addition of 2 integers: " + b);
   }
}
You can also try this code with Online Java Compiler
Run Code


We created a class MethodOverloadingExample in the above example that contains the method addition with the same return types but different number of parameters. We invoked the method addition from the main method with two and three parameters, respectively.

Output

addition of 3 integers: 15

addition of 2 integers: 9 

By changing the sequence/order of parameters

We can overload any method by changing the data types of the parameters we send while invoking the method. 

class MethodOverloadingExample {

   static double addition(double x, int y) {
      return x + y;
   }

   static double addition(int x, double y) {
      return x + y;
   }

   public static void main(String[] args) {
      int a = addition(5.2, 8);
      double b = addition(4, 5.2);
      System.out.println("addition of double and int: " + a);
      System.out.println("addition of int and double: " + b);
   }
}
You can also try this code with Online Java Compiler
Run Code


We created a class MethodOverloadingExample in the above example containing the method addition with the same return types, but the order of parameters is different. We invoked the method addition from the main method with different parameters of int and double.

Output

addition of double and int: 13.2

addition of double and int: 9.2

Must Read Conditional Statements in Java and Hashcode Method in Java.

Frequently Asked Questions

What is method overriding in Java?

Method overriding in Java allows a subclass to provide a specific implementation of a method already defined in its superclass.

What is the real-time use of method overriding?

Method overriding is used in Java to achieve runtime polymorphism, allowing subclasses to tailor a specific behavior to fit their needs.

What is method overloading in Java?

Method overloading in Java allows multiple methods to have the same name but different parameters, enhancing program readability and reusability.

Conclusion

Method overloading in Java enhances code's readability and flexibility by allowing multiple methods with the same name but different parameters within a same class. It reduces the need for redundant method names which leads to cleaner and more maintainable code. Overall, method overloading is a powerful feature in Java that enables developers to write efficient, reusable, and organized code, making it easier to manage and scale applications.

Recommended Readings:

String Args in Java

Why is Java Platform Independent 

Solid Principles in java

Hey Ninjas! Don’t stop here; check out Coding Ninjas for Java, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass