Java Method Overloading

A detailed description with examples of method overloading in Java is provided in order to satisfy educational requirements.

Benefits of Method Overloading in Java

  1. A program that overloads methods is more readable.
  2. It allows programmers to call the same method on different types of data, providing them with flexibility.
  3. The code looks cleaner this way.
  4. Execution takes less time.
  5. Overloading methods reduces code complexity.
  6. The code can then be reused, thereby saving memory.


Method Overloading In Java

It is imperative to understand that method overloading in Java can have multiple methods having different parameters with the same name.

Example

int my_Int_Method(int mrx)
float my_Float_Method(float mrx)
double my_Dobule_Method(double mrx, double ample)

Two methods can be used to add numbers of different types in the following example:

Example: 

public class Main {static int add_Int_Numbers(int mrx,int ample){return mrx+ample;}static double add_Double_Numbers(double mrx,double ample){return mrx+ample;}public static void main(String[] args) {int result_Int_Method=add_Int_Numbers(8,5);System.out.println("Result of int method: "+result_Int_Method);double result_Double_Method=add_Double_Numbers(4.6,6.3);System.out.println("Result of double method: "+result_Double_Method);}// Outputs:// Result of int method: 13 // Result of double method: 10.899999999999999 }

 

It is better to overload one method rather than define two that do the same thing.

The following example overloads the sum_Method to work on both doubles and ints:

Example: 

public class Main {static int sum_Numbers(int mrx,int ample){return mrx+ample;}static double sum_Numbers(double mrx,double ample){return mrx+ample;}public static void main(String[] args) {int result_Int_Method=sum_Numbers(4,2);System.out.println("Result of int sum_Method: "+result_Int_Method);double result_Double_Method=sum_Numbers(3.6,5.3);System.out.println("Result of double_Method: "+result_Double_Method);}// Outputs:// Result of int sum_Method: 6 // Result of double_Method: 8.9 }

Example: 

public class Main {static int Product_Method_Int(int mrx,int ample){return mrx*ample; } static long Product_Method_Long(long mrx,long ample){return mrx*ample;}public static void main(String[] args) {int result1=Product_Method_Int(24,2); long result2=Product_Method_Long(1231433113,454213423);System.out.println("Product of Int_Numbers: "+result1); System.out.println("Product of Long_Numbers: "+result2); } }

As we discussed above, overloading one method is better than defining two that do the same thing.
For longs and integers, the following example overloads the product_Method:

Example: 

public class Main {static int product_Method(int mrx,int ample){return mrx*ample; } static long product_Method(long mrx,long ample){return mrx*ample;}public static void main(String[] args) {int result1=product_Method(24,2); long result2=product_Method(1231433113,454213423);System.out.println("Product of Int_Numbers: "+result1); System.out.println("Product of Long_Numbers: "+result2); } }

Reminder: If the number and type of parameters are different, multiple methods can have the same name.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

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