Introduction
In Java, Method Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters, or a mixture of both.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | // Java program to demonstrate working of method // overloading in Java public class Sum { // Overloaded sum(). This sum takes two int parameters public int sum(int x, int y) { return (x + y); } // Overloaded sum(). This sum takes three int parameters public int sum(int x, int y, int z) { return (x + y + z); } // Overloaded sum(). This sum takes two double // parameters public double sum(double x, double y) { return (x + y); } // Driver code public static void main(String args[]) { Sum s = new Sum(); System.out.println(s.sum(10, 20)); System.out.println(s.sum(10, 20, 30)); System.out.println(s.sum(10.5, 20.5)); } } |
Output:
1 2 3 | 30 60 31.0 |
Different Ways of Method Overloading in Java
- Changing the Number of Parameters.
- Changing Data Types of the Arguments.
- Changing the Order of the Parameters of Methods
Changing the Number of Parameters
Method overloading can be achieved by changing the number of parameters while passing to different methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // Java Program to Illustrate Method Overloading // By Changing the Number of Parameters // Class 1 // Helper class class Product { // Method 1 // Multiplying two integer values public int multiply(int a, int b) { int prod = a * b; return prod; } // Method 2 // Multiplying three integer values public int multiply(int a, int b, int c) { int prod = a * b * c; return prod; } } // Class 2 // Main class public class Wrap{ // Main driver method public static void main(String[] args) { // Creating object of above class inside main() // method Product ob = new Product(); // Calling method to Multiply 2 numbers int prod1 = ob.multiply(1, 2); // Printing Product of 2 numbers System.out.println( "Product of the two integer value :" + prod1); // Calling method to multiply 3 numbers int prod2 = ob.multiply(1, 2, 3); // Printing product of 3 numbers System.out.println( "Product of the three integer value :" + prod2); } } |
Output:
1 2 | Product of the two integer value :2 Product of the three integer value :6 |
Changing Data Types of the Arguments
In many cases, methods can be considered Overloaded if they have the same name but have different parameter types, methods are considered to be overloaded.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // Java Program to Illustrate Method Overloading // By Changing Data Types of the Parameters // Class 1 // Helper class class Product { // Multiplying three integer values public int Prod(int a, int b, int c) { int prod1 = a * b * c; return prod1; } // Multiplying three double values. public double Prod(double a, double b, double c) { double prod2 = a * b * c; return prod2; } } public class Wrap{ public static void main(String[] args) { Product obj = new Product(); int prod1 = obj.Prod(1, 2, 3); System.out.println( "Product of the three integer value :" + prod1); double prod2 = obj.Prod(1.0, 2.0, 3.0); System.out.println( "Product of the three double value :" + prod2); } } |
Output:
1 2 | Product of the three integer value :6 Product of the three double value :6.0 |
Changing the Order of the Parameters of Methods
Method overloading can also be implemented by rearranging the parameters of two or more overloaded methods. For example, if the parameters of method 1 are (String name, int roll_no) and the other method is (int roll_no, String name) but both have the same name, then these 2 methods are considered to be overloaded with different sequences of parameters.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // Java Program to Illustrate Method Overloading // By changing the Order of the Parameters // Class 1 // Helper class class Student { // Method 1 public void StudentId(String name, int roll_no) { System.out.println("Name :" + name + " " + "Roll-No :" + roll_no); } // Method 2 public void StudentId(int roll_no, String name) { // Again printing name and id of person System.out.println("Roll-No :" + roll_no + " " + "Name :" + name); } } // Class 2 // Main class public class Wrap{ // Main function public static void main(String[] args) { // Creating object of above class Student obj = new Student(); // Passing name and id // Note: Reversing order obj.StudentId("Spyd3r", 1); obj.StudentId(2, "Kamlesh"); } } |
Output:
1 2 | Name :Spyd3r Roll-No :1 Roll-No :2 Name :Kamlesh |