Java Method Overloading

Java Method Overloading

What is Java Method Overloading? 

  • Java method overloading allows you to define multiple methods with the same name in the same class, as long as they have different method parameters.
  • This allows you to provide multiple methods with the same name, but different behavior based on the arguments passed to the method.
  • To understand the Java Method Overloading, Read the Complete Article.

Why method overloading?

  • Method overloading in Java provides the following benefits:
    • Readability: Overloading methods with the same name but different parameters can make your code more readable and self-documenting. This can improve code maintainability and make it easier for other developers to understand your code.
    • Flexibility: Overloading allows you to define methods that can perform similar operations on different data types, which makes your code more flexible and adaptable to different situations.
    • Code reusability: Overloading methods reduces the amount of code duplication in your program, as you can reuse method names and their functionality across different parts of your code.
    • Compile-time polymorphism: Method overloading is an example of compile-time polymorphism, which allows Java to choose the appropriate method to execute based on the arguments passed to the method. This can improve the performance of your code by reducing the amount of runtime type checking that is necessary.
Java Method Overloading

Let’s look at the Java Map OverLoading to perform certain operations.

Example 1: Java Program to Overloading by changing the number of parameters

Run
public class Main
{

  public void sum (int a, int b)
  {
    int result = a + b;
      System.out.println ("Sum of two integers: " + result);
  }

  public void sum (int a, int b, int c)
  {
    int result = a + b + c;
    System.out.println ("Sum of three integers: " + result);
  }

  public static void main (String[]args)
  {
    Main obj = new Main ();

    // Call sum() with two arguments
    obj.sum (10, 20);

    // Call sum() with three arguments
    obj.sum (10, 20, 30);
  }
}

Output

Sum of two integers: 30
Sum of three integers: 60

Example 2 : Java Program to Method Overloading by changing the data type of parameters

Run
public class Main
{

  public void display (int num)
  {
    System.out.println ("Integer number: " + num);
  }

  public void display (double num)
  {
    System.out.println ("Double number: " + num);
  }

  public static void main (String[]args)
  {
    Main obj = new Main ();

    // Call display() with an int argument
    obj.display (10);

    // Call display() with a double argument
    obj.display (10.5);
  }
}

Output

Integer number: 10
Double number: 10.5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription