Java Method Overloading

In this lecture, we will see the Java Method Overloading and how we can achieve it with the help of some examples in Java.

What is Java Method Overloading?

When a class has more than methods with the same name but different in parameters, it is called Java method overloading. For Example:

void function() { ... }
void function(int a) { ... }
float function(double a) { ... }
float function(int a, float b) { ... }
Notes: The return types of the all methods are not the same. Because method overloading is not associated with return types. Java Overloaded methods may have the same or different return types, but methods must differ in parameters.

What is the advantage of method overloading in java?

  1. The main advantage of this is cleanliness of code.
  2. Method overloading increases the readability of the program.
  3. Overloaded methods give programmers the flexibility to call a similar method for different types of data.
  4. Overloading is also used on constructors to create new objects given different amounts of data.

Source: Quora.

How to perform method overloading?

There are two ways to overload the method.

  1. Changing number of parameters or arguments.
  2. Changing the data type.

1. By Changing the data type:

In this way of overloading a method in java, we are doing this by changing the data types.


public class MethodOverloading {

	    // this method accepts int
	    private static void display(int a){
	        System.out.println("Got an Integer data.");
	    }

	    // this method  accepts String object
	    private static void display(String a){
	        System.out.println("Got a String object.");
	    }

	    public static void main(String[] args) {
	        display(1);
	        display("Hello");
	    }
	}

Output:

java method overloading

In the above example, we can see that both overloaded methods accept one parameter. However, one method accepts the argument int object and 2nd one accepts a String object.

2. By Changing number of parameters:

In this way of overloading a method in java, we are doing this by changing a number of parameters.

class MethodOverloading {
    private static void display(int a){
        System.out.println("Parameter: " + a);
    }

    private static void display(int a, int b){
        System.out.println("Parameter"+ ": " + a + " and " + b);
    }

    public static void main(String[] args) {
        display(1);
        display(1, 4);
    }
}

Output:

java method overloading

Q. Can we overload the java main() method?

Yes, by method overloading. We can have any number of main methods in a class by method overloading. But JVM calls the main() method which receives string array as arguments only.

Q. Can we declare one overloaded method as static and another one as non-static?

Yes, we can declare one overloaded method as static and another one as non-static.

Q. Is it possible to have two methods in a class with the same method signature but different return types?

No, the compiler will give a duplicate method error. The compiler checks only method signature for duplication, not the return types. If two methods have the same method signature, straight away it gives a compile-time error.

Leave a Reply

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