MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java Method Overloading


In Java, it is possible for a class to contain two or more methods with the same name but with different parameters. Such methods are called Overloaded methods.

Method overloading is used to increase the readability of the program.

void foo()
void foo(int a)	//Overloaded function foo
int foo(int a, int b)

Ways to perform method overloading :


In Java, method overloading can be performed by two ways listed below :

1. By changing the return type of the different methods
2. By changing the number of arguments accepted by the method

Now, let's have an example to understand the above ways of method overloading :

By changing the return type :

In the below example, we've created a class named calculate. In the calculate class, we've two methods with the same name i.e. multiply These two methods are overloaded because they have the same name but their return is different. The return type of 1st method is int while the return type of the other method is double.

class calculate{
     int multiply(int a,int b){
        return a*b;
    }
    double multiply(double a,double b){
         return  a*b;
    }

public static void main(String[] args) {

        calculate obj = new calculate();
        int c = obj.multiply(5,4);
        double d = obj.multiply(5.1,4.2);
        System.out.println("Mutiply method : returns integer : " + c);
        System.out.println("Mutiply method : returns double : " +  d);

}
}

Output

Mutiply method : returns integer : 20
Mutiply method : returns double : 21.419999999999998

By changing the number of arguments passed :


Again, we've created two methods with the same name i.e., multiply. The return type of both the methods is int. But, the first method 2 arguments and the other method accepts 3 arguments.

Example :

class calculate{
     int multiply(int a,int b){
        return a*b;
    }
    int multiply(int a,int b,int c){
         return  a*b*c;
    }

public static void main(String[] args) {

        calculate obj = new calculate();
        int c = obj.multiply(5,4);
        int d = obj.multiply(5,4,3);
        System.out.println(c);
        System.out.println(d);

}
}

Output

20
60


Note: Method overloading cannot be performed by changing the return type of methods.


Example


package com.company;

public class method_overloading {
    static void foo(){
        System.out.println("Good Morning bro!");
    }

    static void foo(int a){
        System.out.println("Good morning " + a + " bro!");
    }

    static void foo(int a, int b){
        System.out.println("Good morning " + a + " bro!");
        System.out.println("Good morning " + b + " bro!");
    }

    static void foo(int a, int b, int c){
        System.out.println("Good morning " + a + " bro!");
        System.out.println("Good morning " + b + " bro!"); 
    }

    static void change(int a){
        a = 98;
    }

    public static void main(String[] args) {
       
        // Method Overloading
        foo();
        foo(3000);
        foo(3000, 4000);
        // Arguments are actual!


    }
}

Output

Good Morning bro!
Good morning 3000 bro!
Good morning 3000 bro!
Good morning 4000 bro!


Conclusion

In this page (written and validated by ) you learned about Java Method Overloading . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Variable Arguments (VarArgs).



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.