Method Overloading in Java

Method overloading, also known as function overloading, is the ability of a class to have multiple methods with the same name, granted that they differ in either number or type of arguments. Compiler checks method signature for method overloading.

Method signature consists of three things :

  • Method name
  • Number of parameters
  • Types of parameters

Code Snippet

// Method Overloading in Java

class MathOperation {
    public static int multiply(int a, int b) {
        return a * b;
    }

    public static double multiply(double x, double y) {
        return x * y;
    }

    public static double multiply(double i, int j) {
        return i * j;
    }

    public static int multiply(int r) {
        return r * r;
    }
}

public class App {
    public static void main(String arg[]) {
        System.out.println("Multiply 2 Integer Value : " + MathOperation.multiply(25, 10));
        System.out.println("Multiply 2 Double Value : " + MathOperation.multiply(2.5, 8.5));
        System.out.println("Multiply Double & Integer Value : " + MathOperation.multiply(2.5, 8));
        System.out.println("Multiply Integer Value : " + MathOperation.multiply(2));
    }
}