Method Overloading Tutorial With Example In JAVA

Method Overloading allows multiple methods to have same name if the parameter list inside parenthesis are different. The parameter list can differ in number of parameter, sequence of data type or type of parameter.

Formal Definition: “If a class has multiple methods by same name but different arguments, it is known as Method Overloading”.


Method Overloading Explanation With Example

Overloading is what, when we use same thing again and again for different purposes, for example in figure below a Donkey is overloaded, same is the case with Method Overloading,

Method Overloading example donkey overloaded
Consider your method as a Donkey, and we are overloading our method, that is we are giving same method different tasks to do based on parameter, and then we say that the method is over loaded.

Need Of Method Overloading:

Firstly let us discuss how Method overloading comes into picture and makes our task much easier.

Let us take an example, suppose we have to add two Integers, then we define a method addTwoIntegers() (which is returning an int value) as shown:

int addTwoIntegers(int a, int b)
{
 int c;

 c=a+b;

 return c;
}

Now, Requirement comes to add three integers, so now we define another method addThreeIntegers() with three arguments as shown: 

int addThreeIntegers(int a, int b, int c)
{
  int d;

  d=a+b+c;

  return d;
}

Now if requirement is to add four integers, then we will again write a method addfourIntegers(), which will make our task of remembering much difficult that which method is for what.

Method overloading is the concept of using same method name, but passing different arguments, which will make our task of remembering much easier. Let us discuss it by taking same example but using method overloading , we will use one method add(), and use it for different tasks.

For adding two Integers we will use same Method add() with two arguments as shown:

int add(int a, int b)
{
  int c;

  c=a+b;

  return c;
}

For adding three Integers we will use same Method add() with three arguments as shown:

int add(int a, int b, int c)
{
  int d;

  d=a+b+c;

  return d;
}

And likewise we can change the number of Arguments in the method to perform different tasks based on the requirement that comes.

Important Note: If the methods have different return type then they are not considered as overloaded methods and hence error message is generated by compiler. The reason behind this is that it is not possible for the compiler to determine which version of the method to call.


Types of Method Overloading:

Method Overloading can be done by following types:

  1. By Changing the number of parameters in the Method.
  2. By Changing the type of parameter in the Method.

Example for overloading a method by changing the number of parameters:

In this example we have defined two overloaded methods, first method adds two Integer values and other adds four Integer values:

public class MethodOverloading
 {

   void add(int a,int b)
  {

    System.out.println(a+b);

  }
  void add(int a, int b, int c, int d)
 {

  System.out.println(a+b+c+d);
 }

public static void main(String args[]){

MethodOverloading obj=new MethodOverloading();

obj.add(10,100);

obj.add(25,35,45,55);

}

}

Output:

110
160

Example for overloading a method by changing the type of parameters:

In this example we have defined two overloaded methods, first method adds two Integers and other adds two float values:

public class MethodOverloading
 {
  void add(int a,int b)
 {

  System.out.println(a+b);

 }

  void add(float a,float b)
 {

  System.out.println(a+b);

 }
public static void main(String args[]){

MethodOverloading obj=new MethodOverloading();

obj.add(15f,15f);

obj.add(25,25);

}

}

Output:

30.0
50

Some important Points regarding Method Overloading:

  • Method Overloading is also called as Compile time Polymorphism or we can say Static Binding. As Compiler checks which method to call based on its number of parameters or type of parameters before running the program.
  • We cannot overload a method only by having different return type and having same number and type of arguments/parameter. As method overloading is a compile time overloading, means it checks which method to call during compile time. At compile time it’s not clear which method to call because its not returning any value. So it gives you a compile time error.

Lets discuss it with example; it will give you Compile time error as shown in comment segment

public class ArrayListMethods {
	
int add(int a,int b) 
{
	 int c;
	 c=a+b;
	 return c;

}  
//Change return type but method has same number and type of parameter

double add(int a,int b) // Giving Compile time error of Duplicate method 
{
	double c;
	 c=a+b;
	 return c;

}  
		  
		  public static void main(String args[]){  
			  
			  ArrayListMethods obj=new ArrayListMethods();
  
		  		System.out.println(add(20,20));
		  
		  }  
		}

Important Interview Question on Method Overloading:

Q: Can we have main() method overloaded?

Ans: Yes, we can have more than one main method having different number and types of arguments.

Lets discuss it by taking an example, in this we have two main methods, 1st main method behave as a normal method and two integers are passed in that, which is returning its sum, and 2nd main method is called by Java Virtual Machine (JVM) during start , and which is calling the above main method as shown:

public class MethodOverloading 
{

 public static void main(int a, int b){

 System.out.println(a+b);

}

public static void main(String args[]){

System.out.println("We are in MAIN Method");

main(5,10);

}

}

Output:

We are in MAIN Method
15

Importance of Method Overloading :

  • Method overloading increases the readability of the code.
  • Makes our task much easier by not remembering any specific name regarding types of parameter and number of parameters.
  • Through method overloading, maintainability of the program becomes easier.
  • We can use overloaded methods extensively for handling class objects.

Quick Revision points:

  • Method overloading is also called Compile time polymorphism or static Binding.
  • Method overloading is attained by having same name, but different number and types of parameters.
  • Java language forbids overloading methods only by return type.

Ask Your Question Or Leave A Reply

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