Method Overloading in Java with Examples (Latest)

1. Objective

In our last tutorial, we discussed Method Overriding in Java. Here, we will learn Method Overloading in Java, different ways by which method overloading can take place and some important point of Java method overloading with examples.

So, let’s start Method Overloading in Java.

What is Method Overloading in Java

What Method Overloading in Java

2. What is Method Overloading in Java?

Overloading is the Java method which allows the methods to have a similar name but with the difference in signatures which is by input parameters on the basis of Java number or type.

Java Method Overloading supports compile (static) polymorphism.

public class Sum
     {
// Overloaded sum(). This sum takes two int parameters
        public int sum(int x, int y)
            {
                return (x + y);
            }
// Overloaded sum(). This sum takes three int parameters
        public int sum(int x, int y, int z)
            {
                return (x + y + z);
            }
// Overloaded sum(). This sum takes two double parameters
       public double sum(double x, double y)
           {
               return (x + y);
           }  
// Driver code
      public static void main(String args[])
          {
              Sum s = new Sum();
              System.out.println(s.sum(10, 20));
              System.out.println(s.sum(10, 20, 30));
              System.out.println(s.sum(10.5, 20.5));
         }
    }

3. Important Points of Method Overloading in Java

a. The advantage of Method Overloading in Java

The advantage of Java method overloading is that it makes us easier for us deal with methods as we don’t have to remember different names for the same operation, we can just change the parameters of Java method.
For example sum1, sum2 etc.

b. Overloading on methods for a return type

We cannot perform method overloading for return type in Java too, as it was in C++.
Example

public class Main
      {
         public int foo()
             {
                 return 10;
             }
// compiler error: foo() is already defined
         public char foo()
            {
                return 'a';
            }
         public static void main(String args[])
    }

c. Overloading of static methods

Java supports overloading of static methods with a different signature.

d. Overloading of the main() method

Java supports overloading of the main() method.
Example 

import java.io.*;
public class Test
     {   
// Normal main()
        public static void main(String[] args)
              {
                 System.out.println("Hi Geek (from main)");
                 Test.main("Geek");
             }
// Overloaded main methods
       public static void main(String arg1)
             {
                System.out.println("Hi, " + arg1);
                Test.main("Dear Geek","My Geek");
             }
       public static void main(String arg1, String arg2)
            {
                 System.out.println("Hi, " + arg1 + ", " + arg2);
            }
     }

Let’s discuss Java String, Methods, and Constructor – Syntax and Example

4. Different Methods of Method Overloading in Java

Java Method overloading can be done by 3 methods:

a. By a number of parameters in two methods.

import java.io.*;
class Addition
      {
// adding two integer values.
          public int add(int a, int b)
               {       
                  int sum = a+b;
                  return sum;
               }     
// adding three integer values.
         public int add(int a, int b, int c)
               {       
                   int sum = a+b+c;
                   return sum;
               }   
        }
class GFG
     {
          public static void main (String[] args)
               {        
                  Addition ob = new Addition();        
                  int sum1 = ob.add(1,2);
                  System.out.println("sum of the two integer value :" + sum1);
                  int sum2 = ob.add(1,2,3);
                  System.out.println("sum of the three integer value :" + sum2);        
              }
     }

b.  By data types of the parameters of methods

import java.io.*; 
class Addition
      {    
// adding three integer values
         public int add(int a, int b, int c)
                {      
                  int sum = a+b+c;
                  return sum;
                }    
// adding three double values
        public double add(double a, double b, double c)
               {        
                   double sum = a+b+c;
                   return sum;
              }
     }
class GFG
     {
          public static void main (String[] args)
            {       
                Addition ob = new Addition();   
                int sum2 = ob.add(1,2,3);
                System.out.println("sum of the three integer value :" + sum2);
                double sum3 = ob.add(1.0,2.0,3.0);
                System.out.println("sum of the three double value :" + sum3);
            }
     }

c. By order of the parameters of methods

import java.io.*; 
class Flair
      {     
          public void flairIdentity(String name,  int id)
              {         
                 System.out.println("flairName :"+ name +" "+"Id :"+ id);
              }     
         public void flairIdentity(int id, String name)
              {         
                 System.out.println("flairName :"+ name +" "+"Id :"+ id);
              }
     } 
class GFG
     {
          public static void main (String[] args)
             {         
                Flair flair = new Flair();        
                flair.flairIdentity("Mohit", 1);
                flair.flairIdentity("shubham", 2);         
            }
     }

5. Conclusion

In this Java tutorial, we learned what is overloading in Java is, the different method overloading in java can be done, some important points on Java method overloading. In the next Java tutorial, we are going to study the difference between Overloading and Overriding in Java. Furthermore, if you have any query, feel free to ask in the comment section.

For reference

Leave a Reply

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