Method overloading in Java

In this post, we will see what is method overloading in java and overloading in java. Some of the programmers refer to it as function overloading in java and method overloading in java example.

Here is the table content of the article will we will cover this topic.
1. What is Method overloading in Java?
2. You can overload by changing the number of arguments/parameters?
3. You can overload by changing the data type of arguments?
4. The Order of the parameters of methods?

5. An important concept of method Overloading?
6. Can we overload static method in java?
7. Can we overload the main method in java?

8. Difference between overloading and overriding

What is Method overloading in Java?

Method overloading in Java means multiple methods having the same name but different in parameters. A class can hold several methods having the same name, but different types/order/number of parameters.

Suppose you want to perform the addition of the given numbers. But user can enter any number of arguments.

The problem without overloading: If you write the method addTwo(int, int) for two parameters, and addThree(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. It may be difficult to track how many methods in class that are performing addition because its name differs.

Solution by method overloading in Java: We can create both the methods with the same name but with the different numbers of parameters. Let’s say we create two methods, first is addition(int, int) and second is addition(int,int,int).

There are three ways to overload the methods:
1. You can overload by changing the number of arguments/parameters.
2. You can overload by changing the data type of arguments.
3. The Order of the parameters of methods.

1. By changing the number of arguments

We can create multiple methods with the same, but the number of arguments/parameters should be different.

Let’s create two methods with the same name but the parameters will be different.

class ExampleOfMethodOverloading 
{
	public void show(String name)
	{
		System.out.println("Name of person = "+name);
	}
	
	Public void show(String name, String address)
	{
		System.out.println("Name of person = "+name+ " and address is = "+ address);
}
	
	public static void main (String [] args) 
	{
		ExampleOfMethodOverloading example = new ExampleOfMethodOverloading();
		// If user providing one parameter then first method called
		example.show("Ram");
		// If user providing two parameter then second method called
		example.show("Ram", "Chandigarh");
		
	}
}

Output:
Name of person = Ram
Name of person = Ram and address is = Chandigarh

2. By changing the type of argument

We can create multiple methods with the same, but the type of argument/parameters should be different.

Let’s create two methods with the same name but the parameter type will be different.

class ExampleOfMethodOverloading 
{
	public void show(String name)
	{
		System.out.println("Name of person = "+name);
	}
	
	public void show(int age)
	{
		System.out.println("Person age is = "+ age);
	}
	public static void main (String [] args) 
	{
		ExampleOfMethodOverloading example = new ExampleOfMethodOverloading();
		// If user providing parameter of String type then first method called
		example.show("Ram");
		// If user providing parameter of int type then second method called
		example.show(25);
		
	}
}

Output:
Name of person = Ram
Person age is = 25

3. The Order of the parameters of methods

You can overload the method by changing the order of parameter. But it is only possible if any method has different types of parameters.

Example:

class ExampleOfMethodOverloading 
{
	public void show(String name, int age)
	{
		System.out.println("Name of person = "+name+ " and age is = "+ age);
	}
	
	public void show(int age, String name)
	{
		System.out.println("Name of person = "+name+ " and age is = "+ age);
	}
	public static void main (String [] args) 
	{
	  ExampleOfMethodOverloading example = new ExampleOfMethodOverloading();
	  // If user providing parameter of String and int  type then first method called
	  example.show("Ram", 25);
	  // If user providing parameter of int and String type then second method called
	  example.show(25, "Ram");
	}
}

Output:
Name of person = Ram and age is = 25
Name of person = Ram and age is = 25
NOTE: You can’t overload by changing the return type of method

Leave a Comment