Method overloading in Java

Walden Systems Geeks Corner Tutorial Method overloading in Java programming tutorial how to Rutherford NJ New Jersey NYC New York North Bergen County
Java Powers the Biggest Technology. Mobile. Web apps. The Internet of Things. Big data. Machine learning. Cloud platform and cloud infrastructure. The next big things are here now, and Java is at the center of them all. Whether you’re developing the robust enterprise back end, building the agile front end, or thriving in a DevOps role, Java skills can up your game. With more than 10 million programmers and 13 billion Java-enabled devices worldwide, Java is the language that connects applications to data, data to people, and people to their digital lifestyle.

Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. Overloading is related to compile time, or static polymorphism. Method Overloading is when multiple methods have the same name but different parameters. It makes the code easier to read when we have to perform only one operation. There are three ways to overload a method in Java, by changing the number of arguments, by changing the the return data type and the number of arguments, or by overloading with type promotion.

Overloading by changing number of arguments

We can overload the method by changing the number of arguments or by changing the data types of the arguments. In this example, we will create a method named sum that will add up the numbers. We will have one method that will take two numbers and add them up and then we will override the method with another method that takes in three parameters. Method overloading will make our job easier since we don't have to keep coming up with different names for a method that does one thing like sum1, sum2, etc. This will also make our code easier to read.

import java.io.*; 
   
class Example
{  
    public int sum(int a, int b)
    { 
        int sum = a+b; 
        return sum; 
    } 

    public int sum(int a, int b, int c)
    { 
           
        int sum = a+b+c; 
        return sum; 
    } 

    public static void main(String args[]) 
    { 
        Example number = new Example(); 
        System.out.println(number.sum(10, 20)); 
        System.out.println(number.sum(10, 20, 30)); 

    } 
       
} 


Overloading by return type and arguments

We can't overload a method by return type only in Java. The behavior is the same in C++. We can, however overload a method by return type if we change the arguments. We can change the argument data types or change the number of arguments to overload a method by return type. In our example, we will overload a method that will return an int or a float depending on what arguments are sent.

class Example2
{  
    public int sum(int a, int b)
    { 
        int sum = a+b; 
        return sum; 
    } 

    public float sum(int a, int b, int c)
    { 
           
        float sum = a+b+c; 
        return sum; 
    } 
    public float sum(float a, int b )
    { 
           
        float sum = a+b+c; 
        return sum; 
    } 


    public static void main(String args[]) 
    { 
        Example2 number = new Example2(); 
        System.out.println(number.sum(10, 20)); 
        System.out.println(number.sum(10, 20, 30)); 
	System.out.println(number.sum(10.3, 20));

    } 
       
} 

Overloading with type promotion

Overloading with type promotion occurs when one datatype is promoted to another datatype if no matching datatype is found. For example, a byte can be promoted to short, int, long, float or double. Below is a diagram that shows what type promotion is available.



When applying method overloading with type promotion, be careful to avoid ambiguity, which will result in a compile time error. Ambiguity exists if there are two methods with the same number of parameters and different data types but there are no matching method. Below are some examples of overloading with type promotion.

Match found

class Example3
{  
    public int sum(int a, long b)
    { 
        int sum = a+b; 
        return sum; 
    } 

    public int sum(int a, int b, int c)
    { 
           
        int sum = a+b+c; 
        return sum; 
    } 

    public static void main(String args[]) 
    { 
        Example3 number = new Example3(); 
        System.out.println(number.sum(10, 20)); 
    } 
       
} 


Ambiguity ( will result in a compile time error )
class Example4
{  
    public int sum(int a, long b)
    { 
        int sum = a+b; 
        return sum; 
    } 

    public int sum(long a, int b)
    { 
           
        int sum = a+b; 
        return sum; 
    } 

    public static void main(String args[]) 
    { 
        Example3 number = new Example2(); 
        System.out.println(number.sum(10, 20)); 
    } 
       
} 


Ambiguity occurs since there are no matching type arguments in the method and each method promotes a similar number of arguments.