Method Overloading in Java

In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different Parameters (implementations).

Method overloading is a feature that allows us to have more than one method with the same name with different parameters.

public class MethodOverloading {
    public static void main(String[] args) {
        System.out.println(multiplyByTwo("jay", 500));
        System.out.println(multiplyByTwo(500));
    }

    public static int multiplyByTwo(String name, int score) {
        System.out.println("Edited by " + name);
        return score * 2;
    }

    public static int multiplyByTwo(int score) {
        return score * 2;
    }
}
Edited by jay
1000
1000
  • MultiplyByTwo Method is an Overloading Method.
    • multiplyByTwo(String name, int score)
    • multiplyByTwo(int score)

The built-in method PrintIn is also Overloading Method in Java.

public class MethodOverloading {
    public static void main(String[] args) {
        System.out.println(1);
        System.out.println(1.1);
        System.out.println("string");
    }
}
  • A printIn method with different parameters (integer, double, string, and more)

Benefit of Method Overloading

It improves code readability and re-usability.

Leave a Reply

Your email address will not be published.

ANOTE.DEV