Method Overloading in java

Method Overloading in Java

Function method overloading in the Java programming language is a special feature of the Java language that allows a user-declared class in a Java program to have more than one class method with the same name but multiple different class function parameter lists. This is helpful in using and explaining the polymorphism feature in the Java language.

Method Overloading in java

Key Features of Java Method Overloading.

  • Same method name – User-declared class function methods must have the same name in a Java program.
  • Different parameter lists – Variable parameter arguments in a declared user-class function method must be of different numbers, data types, or both.
  • Return type – The return data type in a user-declared function type can be multiple or different, so the programmer cannot differentiate between overloaded methods based on this alone.
  • Access modifiers – In a Java function, users can use multiple different access modifiers, such as public, private, etc., for the data type parameters of an overloaded method, as needed. However, these have no significant impact on method overloading.

Why and how to use method overloading in Java?

  • Code readability – By using the same class function method names for common tasks in a Java program, such as multiple inputs, Java user programs can make the source code more readable and easier to understand than modular programs.
  • Avoiding long method names – Instead of creating multiple methods with multiple different names in a Java program, Java users can create long names and modify the function method parameters only when needed.

How does method overloading work in Java?

When a function method is called in a Java program, Java uses the number of arguments passed to the declared method and its data type to decide which overloaded version of the method to run.

Syntax of Java Method Overloading.

<return type> <method name>(<parameter list>) {

// function Method body

}

Java Method Overloading Key Elements.

  • For an overloaded method in a Java program, the method signature (function method name and parameter data types) must be multiple or different.
  • In a Java program, the return data type is not considered an important element of the method signature, so Java users cannot overload a function method by simply modifying the function return type.

Java Method Overloading Example.

Here’s a simple example to explain Java method overloading with a function method named ‘total’.

public class Main

{

// Here we declare a method to total two different integer values.

public static int total(int p, int q) {

return p + q;

}

/ Here it is an overloaded method to total three integer values.

public static int total(int p, int q, int r) {

return p + q + r;

}

/ Here it is an overloaded method to total two doubles.

public static double total(double p, double q) {

return p + q;

}

public static void main(String[] args) {

/ Here it is calling the overloaded total method with different user-declared arguments.

System.out.println(“The total is – ” + total(1, 7)); // Here it calls the total(int, int) function value

System.out.println(“The total is – ” + total(7, 1, 9)); // Here it calls the total(int, int, int) three integer function value

System.out.println(“The total is – ” + total(2.4, 4.7)); // Here it calls the total function method (double, double) data type

}

}

Explanation of Java Method Overloading.

Here we have declared three different function total methods, where all the function method names are the same, but the function data type parameters and variable argument lists are different.

total(int p, int q)

total(int p, int q, int r)

total(double p, double q)

When the total method in this program is called in the program, Java automatically selects the correct version of the total method based on the passed function parameter arguments.

Leave a Reply