Method Overloading in Java with Example [Updated]

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In Java Polymorphism, we heard the term Method Overloading which allows the methods to have a similar name but with the difference in signatures which is by input parameters on the basis of number or type. Method Overloading in Java supports compile-time (static) polymorphism.

In this article, we will talk about Method Overloading with its rules and methods. We will discuss each and every concept with an example for a clear understanding. So, let’s start!

Example of Methods Overloading in Java-

class Calculator
{
  int addition(int operand1, int operand2)
  {
    return operand1+operand2;
  }
  int  addition(int operand1, int operand2, int operand3)  
  {
    return operand1+operand2+operand3;
  }
}
public class CompileTimePolymorphism
{
  public static void main(String args[])
  {
    Calculator obj = new Calculator();
    System.out.println("Addition of two operands is "+obj.addition(10, 20));
    System.out.println("Addition of three operands is "+obj.addition(10, 20, 30));
  }
}

Output-

Overloading in Java

Different Methods of Method Overloading in Java

Method overloading can be done by 3 methods in Java:

1. By a number of parameters in two methods.

The following example shows how method overloading is done by using a different number of parameters

class Calculator
{
  int addition(int operand1, int operand2)
  {
    return operand1+operand2;
  }
  int  addition(int operand1, int operand2, int operand3)  
  {
    return operand1+operand2+operand3;
  }
}
public class CompileTimePolymorphism
{
  public static void main(String args[])
  {
    Calculator obj = new Calculator();
    System.out.println("Addition of two operands is "+obj.addition(10, 20));
    System.out.println("Addition of three operands is "+obj.addition(10, 20, 30));
  }
}

Output-

Overloading in Java

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Recommended Reading – DataTypes in Java with Examples

2. By data types of the parameters of methods

In the following example, method addition() is overloaded based on the data type of parameters – We have two methods with the name addition(), one with the parameter of int type and another method with the parameter of string type.

class Calculator
{
  void addition(int operand1, int operand2)
  {
    System.out.println(operand1+operand2); 
  }
  void  addition(String alphabet)  
  {
    System.out.println(alphabet); 
  }
}
public class CompileTimePolymorphism
{
  public static void main(String args[])
  {
    Calculator obj = new Calculator();
    obj.addition(10, 20);
    obj.addition("DataFlair");
  }
}

Output-By-data-types-of-the-parameters-of-methods

3. By order of the parameters of methods

Here both methods have a different sequence of data type in an argument list. The first method is having argument list as (String, char) and second is having (int, String, char). Since the sequence is different, the method can be overloaded without any errors.

It’s right time to explore about Command Line Argument in Java

public class MethodOverloadingExample2
 {
  void record(String studentName,char grade)
  {
    System.out.println("Student name is "+studentName);
    System.out.println("Student grade is "+grade);
  }
  void record(int id,String studentName,char grade)
  {
    System.out.println("Student ID is "+id);
    System.out.println("Student name is "+studentName);
    System.out.println("Student grade is "+grade);
  }
  public static void main(String[]args)
  {
    MethodOverloadingExample2 Obj=new MethodOverloadingExample2();
    Obj.record("Renuka",'B');
    Obj.record(8, "Bhumika", 'A');;

  }
}

Output-

Method-Overloading-Example

Important Points of Method Overloading in Java

Advantage of Method Overloading

The advantage of method overloading in Java is that it makes 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 a method.

Have you heard about Method Overriding in Java?

Overloading on methods for a different return type

We cannot perform method overloading for different return types in Java.

Example-

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

Overloading of the main() method

Java supports overloading of the main() method.

Example –

public class MetodOverloadingExample1
{
  // Normal main()
  public static void main(String[] args)
  {
    System.out.println("Hello Readers, Welcome to DataFlair");
  }
  // Overloaded main methods
  public static void main(String arg1)
  {
    System.out.println("Hi, " + arg1);
    MetodOverloadingExample1.main("DataFlair");
  }
  public static void main(String arg1, String arg2)
  {
    System.out.println("Hi, " + arg1 + ", " + arg2);
  }
}

Output-Metod-Overloading-Program in Java

Benefits of Method Overloading in Java

Method overloading offers several advantages in Java programming:

1. Improved Code Readability: By using the same method name for related operations with distinct parameter sets, you enhance code readability. It becomes clear to developers that these methods perform similar tasks but cater to different data types or numbers of arguments.

2. Increased Code Maintainability: Overloading promotes code maintainability as you can modify an overloaded method without affecting code that relies on other versions of the method with different parameter lists.

3. Enhanced Flexibility: Method overloading allows you to design methods that can handle various data types or argument combinations, making your code more adaptable to different use cases.

Summary

Wrapping up, method overloading can be done by 3 methods – by the number of parameters in two methods, data types of the parameters, and order of parameters of methods. It is not an easy task, you have to remember some important points, which we discussed above.

Don’t forget to check Constructor Overloading in Java

In order to give suggestions and feedback, please approach our comment section.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

courses

1 Response

  1. anonymous says:

    Pages of helpful information. Thanks

Leave a Reply

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