DEV Community

Sakthi S
Sakthi S

Posted on

What is Method Overloading in Java?

Method Overloading

  • In java is a feature that allows a class to have multiple methods with the same name, provide they have different parameter lists

  • It is a form of compile-time polymorphism also known as static polymorphism or early binding

  • Methods can share the same name if their parameter lists differ.

  • Cannot overload by return type alone; parameters must differ.

  • The compiler chooses the most specific match when multiple methods could apply.

`

class Product{

public int multiply(int a, int b){

    int prod = a * b;
    return prod;
}


public int multiply(int a, int b, int c){

    int prod = a * b * c;
    return prod;
}
Enter fullscreen mode Exit fullscreen mode

}

class Geeks{

public static void main(String[] args)
{

    Product ob = new Product();


    int prod1 = ob.multiply(1, 2);


    System.out.println(
        "Product of the two integer value: " + prod1);


    int prod2 = ob.multiply(1, 2, 3);


    System.out.println(
        "Product of the three integer value: " + prod2);
}
Enter fullscreen mode Exit fullscreen mode

}`

Output

Product of the two integer value: 2
Product of the three integer value: 6

Enter fullscreen mode Exit fullscreen mode

Top comments (0)