Home >>Java Tutorial >Method Overloading in Java

Method Overloading in Java

Method Overloading in Java

Method Overloading is a condition in which a class consists of multiple methods that are having the exact same name but have different parameters. This is somehow beneficial in the case where the programmer have to perform only one operation that have the exact same name of the methods then it will automatically increases the readability of the program.

For instance, let take a case where the programmer have to perform addition of the provided numbers but there can be any number of the arguments and if the programmer write the method like a(int,int) for two parameters and b(int,int,int) for three parameters then it will be difficult for the programmer as well as the other programmers in order to understand the behavior of the method as the name will differ. Hence, the method overloading is performed in order to quickly judge the program

Advantage of method overloading

There are many advantages of the Method overloading in Java language but the most important of all the advantages is that it enhances the readability of the program.

Different ways to overload the method

There are generally two ways to overload the method in the language java that are depicted below:

  • Changing the data type
  • Changing number of arguments

1. Method Overloading: changing data type of arguments

Here is an example of the method overloading by the changing the data types of the arguments. In this following example, there are two methods being created that generally are of different data types. Here in this example, the first add method will receive two integer arguments and the second add method will receive two double arguments. Go through the example carefully as it will give you an insight to the application aspect of it:

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));

  }
} 
Output:
22
24.9

2. Method Overloading: changing no. of arguments

Here is an example of the method overloading in the Java language where two methods have been created, and the first operation consists of add() method that performs addition of two numbers and then the second add method performs addition of all the three numbers. In the following example, static methods have been created in order to remove the need to create instance for the purpose of 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));

}} 
Output:
22
33

Is Method Overloading possible by changing the return type of method only in the Java language?

In the Java language, the method overloading is not possible just by changing the return type of the method only because of the fact that ambiguity will occur. Here is an example that will clear the fact by depicting the occurrence of the ambiguity:

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  
}} 
Output:
Main.java:9: error: method add(int,int) is already defined in class Adder
static double add (int a, int b)

Difference between the method overloading and the method overriding in Java Language

There are many differences between that are present between the method overloading and the method overriding in java. Here is a list of difference that is being depicted below:

Method Overloading Method Overriding
Method overloading is generally used in the Java language in order to increase the readability of the program. Method overloading is generally used in the Java language in order to increase the readability of the program. Method overriding is generally used in the Java language to provide the specific implementation of the method that has already been provided by its super class.
Method overloading is generally performed within the class. Method overriding generally occurs in between two classes that have a IS-A (inheritance) relationship.
Parameter must be different in terms of method overloading. Parameter must be same in terms of method overriding.
Method overloading is basically known as the example of compile time polymorphism. Method overriding is basically known as the example of run time polymorphism.
In java language, the method overloading can't be performed just by changing the return type of the method only. Return type in the Java language can be same or different in the terms of method overloading. But the programmer has to change the parameter. Return type in the Java language must be the same or covariant in the terms of method overriding.

No Sidebar ads