Method Overloading in Java

Method overloading in java allows multiple methods to have the same name but with different numbers or types of parameters. Methods return type may or may not match.

Example
 void method(); // 1st
 void method(int arg); // 2nd
 void method(int arg1, int arg2); // 3rd

Return Type in Overloading

In java method overloading, the return type of method is not considered. Only the number of arguments or their data types are taken into account. In the above example, if we add another method with the same name but a different return type, the java compiler will throw a Duplicate Method error.

void method(); // Duplicate method
int method(); // Duplicate method
int method(int arg);
int method(int arg1, int arg2);

Below are the correct ways to do method overloading of a single method name.

	// valid ways to do method overloading

	// original function
	public void func(int i, float j) {
	}

	// changing the position of arguments
	public void func(float j, int i) {
	}

	// changing the type of both the arguments to "float"
	public void func(float i, float j) {
	}

	// changing the type of both the arguments to "int"
	public void func(int i, int j) {
	}

	// increasing the number of arguments
	public void func(int i, int j, int k) {
	}

And below are the wrong ways of doing method overloading.

	// invalid ways to do method overloading

	// original function
	public void func(int i, float j) {
	}

	// just changing the return type, as overloading doesn't work for the return type, and thus below statement has errors
	public int func(int i, float j) {
	}
Example

Now let’s see how to do method overloading with an example. In this, we will add two numbers where the numbers can be of type int, double, or long. Instead of using different names for each method, we will use the same name to demonstrate the method overloading.

package com.codekru.model;

public class Sum {

    int sum(int number1, int number2) {
    	System.out.print("int method called: ");
        return number1 + number2;
    }

    long sum(long number1, long number2) {
    	System.out.print("long method called: ");
        return number1 + number2;
    }

    double sum(double number1, double number2) {
    	System.out.print("double method called: ");
        return number1 + number2;
    }

    public static void main(String[] args) {
        Sum s = new Sum();
        System.out.println(s.sum(1, 2));  // method with int parameters will be called
        System.out.println(s.sum(4L, 5L));  // method with long parameters will be called
        System.out.println(s.sum(4.0, 5.0)); // method with oouble parameters will be called
    }
}

Output –

int method called: 3
long method called: 9
double method called: 9.0

Here, in the case of int numbers, the sum method with int parameters gets called,; the same happens in the case of long and double.

Type Conversion in Overloading

If the compiler does not find the exact match for a method call, it tries to find the next higher argument datatype for that method and calls it, but if it doesn’t find any such method too, it will throw an error.

Example
  • Let’s assume we don’t have the method with int parameters in the above example, and see what happens.
package com.codekru.model;

public class Sum {

    long sum(long number1, long number2) {
    	System.out.print("long method called: ");
        return number1 + number2;
    }

    double sum(double number1, double number2) {
    	System.out.print("double method called: ");
        return number1 + number2;
    }

    public static void main(String[] args) {
        Sum s = new Sum();
        System.out.println(s.sum(1, 2));  // method with long parameters will be called
        System.out.println(s.sum(4L, 5L));  // method with long parameters will be called
        System.out.println(s.sum(4.0, 5.0)); // method with double parameters will be called
    }
}

Output –

long method called: 3
long method called: 9
double method called: 9.0

Here, the int parameters have been promoted to the long data type; thus, the method with long datatype arguments has been called. And if we’re only left with the method with double parameters, then that method would have been called for all of the method calls ( int, long and double ).

The below image depicts a representation of the Type Promotion in Java. So, if the method with int parameters is not found, then the compiler will search for the method with long parameters, and if that is also not found, then it will search for the method with float parameters and so on.

Type promotion in Java

Note: Please remember that the automatic conversion can only be done with a larger data type; thus, the below program will throw a compilation error.

package com.codekru.model;

public class Sum {

    int sum(int number1, int number2) {
    	System.out.print("int method called: ");
        return number1 + number2;
    }

    public static void main(String[] args) {
        Sum s = new Sum();
        System.out.println(s.sum(1, 2)); 
        System.out.println(s.sum(4L, 5L));  // this will throw an error now
    }
}

Output –

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method sum(int, int) in the type Codekru is not applicable for the arguments (long, long)

Some Interesting things about Method overloading

Q – Does Java support Operator Overloading?
No, Unlike in C++, we cannot overload the operators in Java. However, there are operators in Java that are internally overloaded. For example, the + operator in Java can be used to add numbers and concatenation of strings.

Q – Can we overload the static Methods?
Yes, we can overload the static methods.

Q – Can we overload the methods which differ by the static keyword?
No, we cannot do that.

Q – Can we overload the constructors in Java?
Yes, we can also overload the constructors just like we do with methods in Java.

Q – Can we overload the main method in Java?
Yes, like every other method, we can also overload the main method of Java, but JVM will only call the original main method first, not the overloaded ones. We have written more about this in another article.

Do you know about the ambiguity while using method overloading? If you want to know about it, you can read this article.

Related Articles

Thank you for reading this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *