๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Method Overloading in Java  


Introduction
  • Method Overloading is a way (or mechanism or form) to achieve compile-time polymorphism
  • It is a feature in Java that allows a class to have more than one method with the same name, but different parameters (number, type, or order of parameters).
  • The compiler determines which method to execute at compile time, based on the method signature.
  • Rules of Method Overloading :-
    • All overloaded methods must have the same name.
    • All the methods should be in same class or in subclass.
    • All the method parameters list must be different:
      • Number of parameters
      • Type of parameters
      • Order of parameters
  • Example :-
    class Calculator
    {
        int add(int a, int b)
        {
            return a + b;
        }
    
        double add(double a, double b)
        {
            return a + b;
        }
    
        int add(int a, int b, int c)
        {
            return a + b + c;
        }
    }
    public class MainApp
    {
        public static void main(String[] args)
        {
            Calculator calc = new Calculator();
    
            // Calling overloaded methods
            int result1 = calc.add(10, 20);
            double result2 = calc.add(5.5, 4.5);
            int result3 = calc.add(1, 2, 3);
    
            // Printing the results
            System.out.println("Result of add(int, int): " + result1);
            System.out.println("Result of add(double, double): " + result2);
            System.out.println("Result of add(int, int, int): " + result3);
        }
    }
    Output:
    Result of add(int, int): 30
    Result of add(double, double): 10.0
    Result of add(int, int, int): 6
Advantages of Method Overloading :-
  1. Increased Readability:
    • Using the same method name for similar actions makes the code easier to understand.
  2. Faster Execution (at Compile Time):
    • Since the method to be called is determined at compile time, it improves performance over runtime decisions (like in method overriding).
  3. Easy Maintenance:
    • Having logically grouped methods under one name makes code more organized and easier to maintain.
  4. Encourages DRY Principle (Don't Repeat Yourself):
    • Avoids writing similar code in different method names for similar logic.
  5. Helps in Testing:
    • Easier to write unit tests for overloaded methods as they often represent the same action with varied input.
  6. Cleaner API Design:
    • When designing libraries or APIs, overloading allows a consistent interface for different data inputs.

Important Points
  • Some important points to remember about method overloading in Java:
    1. Method overloading does not depend on return type, it depends only on the parameter list.
      int add(int a, int b) { return a + b; }
      double add(double a, double b) { return a + b; } //Valid overload
    2. Main method can also be overloaded in Java.
      public static void main(String[] args)
      {
          System.out.println("Main method with String[]");
      }
      public static void main(int[] args)
      {
          System.out.println("Main method with int[]");
      }
    3. Constructors can also be overloaded.
      class Student
      {
          Student() {}
          Student(String name) {}
          Student(String name, int age) {}
      }
    4. Access modifiers (e.g., public, private) can be different.
      public void show() {}
      private void show(int a) {}  //Valid overload
    5. Static methods can be overloaded.
      static void display() {}
      static void display(String msg) {}  //Valid overload