Site icon JavaGoal

Method overloading in Java

Let’s discuss, what 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 When to use Polymorphism?
6. Why ? we can’t overload by changing the return type of the method?
7. Can we overload the static method in java?
8. Can we overload the main method in java?

Video available in Hindi

What is Method overloading in Java?

Method overloading in Java means multiple methods having the same name but different parameters. When 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 the 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 the class are performing addition because its name differs.

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

You can overload the method by changing the order of parameters. 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

An important concept of method Overloading
When to use Polymorphism?

Let’s discuss method overloading in java example and see what will be the outcome.

Suppose there are two methods having the same name but different types of parameters. The parameters are non-primitive types and we are calling the method by a null value. So both methods can accept the null value. But which one gets called?

public class MainClass 
{
    public static void main(String[] arg) 
    {
    	MainClass obj = new MainClass();
    	obj.show(null);
    }
    
    public void show(Object obj)
    {
    	System.out.println("Method with object parameter");
    }
    
    public void show(String obj)
    {
    	System.out.println("Method with String parameter");
    }
}

Output: Method with String parameter

Reason: Java will always try to use the most specific applicable version of a method that’s available. Here Object class is the superclass of String class and String is more specific here.

Why ? we can’t overload by changing the return type of the method


If we create two methods with the same name but different return types. Then the program will be a show compilation error. Let’s see method overloading in the java example with a different scenario.

Example:

class ExampleOfMethodOverloading 
{
	public void show(String name)
	{
		System.out.println("Name of person = "+name);
	}
	
	public boolean show(String name)
	{
		System.out.println("Person name is = "+ name);
	}
	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("Sham");
	}
}

Here, the compiler can’t determine which show() method should be called.

Can we overload the static method in java?

Yes, we can overload the static method. A class can have more than one static method with the same name, but different input parameters. You can overload the static method by changing the number of arguments or by changing the type of arguments. There are many ways to overload a method in Java you can read them from here: Ways to overload the methods?

Example:

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

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

Can we overload methods that differ only by static keyword?

No, we can’t overload the method if they differ only by static keyword. If we are defining two methods with the same name and parameters but here we are differentiating them by static keyword. Then the compiler shows an error.

public class MainClass 
{
	public void printData()
	{
		System.out.println("Print data method without static keyword");
	}
	
	public static void printData()
	{
		System.out.println("Print data method without static keyword");
	}
	
	public static void main(String arg[])
	{
		// Body of main method
	}
}

Can we overload the main method in java?

Yes, we can overload the main method, and here we will see the overloading main method in java.

Do most beginners have a common doubt about whether it’s possible to overloading main method in java? How does work if we overload main method in java? Which method gets the call first? In this article, we will resolve all the ambiguities.

Overloading the main method in java

The main() method is a special method because it’s the entry point of the java application. The JVM will always call the main method to start the application. But as we said it’s a method so like any other method, we can overload it. You can overload the main method as per the method of overloading rules. You can overload it by the number of parameters or the type of parameters or both.
Here we will see how to overloading main method in java. We will overload the method in different ways.

public class OverloadMainMethod 
{
	// It is main method with String type of parameters
	// It is entry point for JVM
	public static void main(String arg[])
	{
		System.out.println("Main method having parameter  of String type");
	}
	
	// It is overload form of main method with String type of parameters
	public static void main(String arg[], String b)
	{
		System.out.println("Main method having parameter  of String type");
	}
	
	// It is overload form of main method with int type of parameters
	public static void main(int a)
	{
		System.out.println("Main method having parameter of int type");
	}
}

Output: Main method having parameter of String type

In the above example, we are overloading the main() method. Here we define two more methods with different types of parameters. You must have noticed the JVM calling the original main() method that is defined with  String type of parameters (String arg[]). It is not called the overloaded main method.

How JVM works when the main() is overloaded?

When the user does the action to start the application, The JVM always looks for the main method with its signature to launch the program. If the main() method is not defined with String parameters (String[] args) the JVM, is not able to run it.

In the above example, we have overloaded the main method and defined two more main methods with different signatures. But the JVM executes the original main method, we can call the overloaded main method from the actual main method only. 

Exit mobile version