When two methods in the same class have the same name but different arguments This is an example of?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example:

void func() { ... } void func(int a) { ... } float func(double a) { ... } float func(int a, float b) { ... }

Here, the func() method is overloaded. These methods have the same name but accept different arguments.

Note: The return types of the above methods are not the same. It is because method overloading is not associated with return types. Overloaded methods may have the same or different return types, but they must differ in parameters.

Why method overloading?

Suppose, you have to perform the addition of given numbers but there can be any number of arguments (let’s say either 2 or 3 arguments for simplicity).

In order to accomplish the task, you can create two methods sum2num(int, int) and sum3num(int, int, int) for two and three parameters respectively. However, other programmers, as well as you in the future may get confused as the behavior of both methods are the same but they differ by name.

The better way to accomplish this task is by overloading methods. And, depending upon the argument passed, one of the overloaded methods is called. This helps to increase the readability of the program.

How to perform method overloading in Java?

Here are different ways to perform method overloading:

1. Overloading by changing the number of parameters

class MethodOverloading { private static void display(int a){ System.out.println("Arguments: " + a); } private static void display(int a, int b){ System.out.println("Arguments: " + a + " and " + b); } public static void main(String[] args) { display(1); display(1, 4); } }

Output:

Arguments: 1 Arguments: 1 and 4

2. Method Overloading by changing the data type of parameters

class MethodOverloading { // this method accepts int private static void display(int a){ System.out.println("Got Integer data."); } // this method accepts String object private static void display(String a){ System.out.println("Got String object."); } public static void main(String[] args) { display(1); display("Hello"); } }

Output:

Got Integer data. Got String object.

Here, both overloaded methods accept one argument. However, one accepts the argument of type int whereas other accepts String object.

Let’s look at a real-world example:

class HelperService { private String formatNumber(int value) { return String.format("%d", value); } private String formatNumber(double value) { return String.format("%.3f", value); } private String formatNumber(String value) { return String.format("%.2f", Double.parseDouble(value)); } public static void main(String[] args) { HelperService hs = new HelperService(); System.out.println(hs.formatNumber(500)); System.out.println(hs.formatNumber(89.9934)); System.out.println(hs.formatNumber("550")); } }

When you run the program, the output will be:

500 89.993 550.00

Note: In Java, you can also overload constructors in a similar way like methods.

Recommended Reading: Java Constructor Overloading

Important Points

  • Two or more methods can have the same name inside the same class if they accept different arguments. This feature is known as method overloading.
  • Method overloading is achieved by either:
    • changing the number of arguments.
    • or changing the data type of arguments.
  • It is not method overloading if we only change the return type of methods. There must be differences in the number of parameters.

I am aware of the fact that a Java method can be overloaded in same class or in a subclass.

Correct. A method is considered to be overloaded when multiple functions within the same class share the same name but differ by their arguments.

public class A { public void foo(int param) { } public void foo(string param) { } }

But can I overload a Java static method in a non-subclass ?

Yes you can. A subclass is a class that inherits attributes from a parent class (also known as a superclass). There is no rule against method overloading if a class is a subclass, not a subclass or if the methods are static.

public class A { public static void foo(int param) { } public static void foo(string param) { } }

However the above code in your question is not an example of method overloading, because the methods with the same name (main) are not contained within the same class/scope. They do however differ by their argument lists, but that is not really of any consequence.

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

If we have to perform only one operation, having same name of the methods increases the readability of the program.

Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs.

So, we perform method overloading to figure out the program quickly.

When two methods in the same class have the same name but different arguments This is an example of?

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

  1. By changing number of arguments
  2. By changing the data type

In Java, Method Overloading is not possible by changing the return type of the method only.

1) Method Overloading: changing no. of arguments

In this example, we have created two methods, first add() method performs addition of two numbers and second add method performs addition of three numbers.

In this example, we are creating static methods so that we don't need to create instance for calling methods.

class Adder{ static int add(int a,int b){return a+b;} static int add(int a,int b,int c){return a+b+c;} } class TestOverloading1{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(11,11,11)); }}

Test it Now

Output:


2) Method Overloading: changing data type of arguments

In this example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments.

class Adder{ static int add(int a, int b){return a+b;} static double add(double a, double b){return a+b;} } class TestOverloading2{ public static void main(String[] args){ System.out.println(Adder.add(11,11)); System.out.println(Adder.add(12.3,12.6)); }}

Test it Now

Output:

Q) Why Method Overloading is not possible by changing the return type of method only?

In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Let's see how ambiguity may occur:

class Adder{ static int add(int a,int b){return a+b;} static double add(int a,int b){return a+b;} } class TestOverloading3{ public static void main(String[] args){ System.out.println(Adder.add(11,11));//ambiguity }}

Test it Now

Output:

Compile Time Error: method add(int,int) is already defined in class Adder

System.out.println(Adder.add(11,11)); //Here, how can java determine which sum() method should be called?

Note: Compile Time Error is better than Run Time Error. So, java compiler renders compiler time error if you declare the same method having same parameters.

Can we overload java main() method?

Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. Let's see the simple example:

class TestOverloading4{ public static void main(String[] args){System.out.println("main with String[]");} public static void main(String args){System.out.println("main with String");} public static void main(){System.out.println("main without args");} }

Test it Now

Output:

Method Overloading and Type Promotion

One type is promoted to another implicitly if no matching datatype is found. Let's understand the concept by the figure given below:

When two methods in the same class have the same name but different arguments This is an example of?

As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int,long,float or double and so on.

Example of Method Overloading with TypePromotion

class OverloadingCalculation1{ void sum(int a,long b){System.out.println(a+b);} void sum(int a,int b,int c){System.out.println(a+b+c);} public static void main(String args[]){ OverloadingCalculation1 obj=new OverloadingCalculation1(); obj.sum(20,20);//now second int literal will be promoted to long obj.sum(20,20,20); } }

Test it Now

If there are matching type arguments in the method, type promotion is not performed.

class OverloadingCalculation2{ void sum(int a,int b){System.out.println("int arg method invoked");} void sum(long a,long b){System.out.println("long arg method invoked");} public static void main(String args[]){ OverloadingCalculation2 obj=new OverloadingCalculation2(); obj.sum(20,20);//now int arg sum() method gets invoked } }

Test it Now

Output:int arg method invoked

If there are no matching type arguments in the method, and each method promotes similar number of arguments, there will be ambiguity.

class OverloadingCalculation3{ void sum(int a,long b){System.out.println("a method invoked");} void sum(long a,int b){System.out.println("b method invoked");} public static void main(String args[]){ OverloadingCalculation3 obj=new OverloadingCalculation3(); obj.sum(20,20);//now ambiguity } }

Test it Now

Output:Compile Time Error

Next TopicMethod Overriding in java