What is Method Overloading in Java

Method overloading is one of the important feature in java and is quite used in java programming. It is quite often asked with java programmers specially with beginners in interviews. This tutorial covers different details about method overloading along with some questions which will clear your doubts about method overloading.

What is method overloading

Method overloading is a technique in java which allows java programmers to write two or more than two methods with same name but different in parameters. It simply means in java a class can have multiple methods with same name but different in parameters, all such methods are called overloaded methods. Below code shows an example of method overloading in java :

 class Addition {
    int add(int a, int b) {  }
    int add(int a, int b, int c) {  }
    double add(double a, int b) {  }
 }   

Here add method is an overloaded method since it is more than once. You can see each add method differs with other add method either in number of parameters or data type of parameters.

Does return type or access modifier of a method plays role in method overloading ?

No neither return type nor access modifier of a method plays role in method overloading. Compiler will throw error if you define two methods with same parameters but different return type or access modifier in your class.

Parameter rules for method overloading in Java

Each overloaded method must differ with other in one or both the parameter rules given below :

  • The number of parameters in both methods are different.
  • Data type of parameters in both methods are different.

So it's only parameter that differentiates one overloaded method with other. Return type or access modifier of a method does not matter in method overloading.

How I should compare the parameters of methods ?

Compare the parameters of two method in sequential order which means first parameter of first method with first parameter of second method, second parameter of first method with second parameter of second method and so on. The overloaded method must differ in one or both rules defined above.

Real time example of method overloading

Many of our real world habits like speak, teach, walk etc are polymorphic or overloaded in nature. For example when we are happy we speak softly but when we are angry we speak differently. Here the same behavior speak is different with different inputs(happy and angry).

Similarly when we teach to student of a class we teach professionally but when we teach to a kid at home we teach differently, so the same action teach has different behavior in different situations or inputs.

Method overloading program in Java

 class MethodOverloading {    
    int add(int a, int b) {
       return (a + b);     
     } 
     int add(int a, int b, int c) {
       return (a + b + c);      
     }
     double add(double a, double b) {
       return (a + b);      
     }
     public double add(int a, double b) {
       return (a + b);      
     }
       
    public static void main(String [] args) {
      int sum1, sum2;
      double sum3, sum4;
      MethodOverloading obj = new MethodOverloading();       
      sum1 = obj.add(20,30);
      sum2 = obj.add(20,30,40); 
      sum3 = obj.add(20.5,30.5);
      sum4 = obj.add(20,30.5);      
      
      System.out.println("sum1 = "+sum1+" sum2 = "+sum2);  
      System.out.println("sum3 = "+sum3+" sum4 = "+sum4);  
    }
 }

Output:

sum1 = 50 sum2 = 90
sum3 = 51.0 sum4 = 50.5

Here add method has four different versions, all of them differs with each other either or both, in number of parameters or data type of parameters.

Can we overload main method in java ?

Yes you can overload main method any number of times by following the rules described above. At runtime java virtual machine calls the main method having a single parameter of string array type.

Can we overload constructors as we overload methods ?

Yes we can overload constructor as well. Constructor overloading is also a useful concept in java.

Use of method overloading in Java

When it comes to why or what is the use of method overloading in java, following points comes in mind :

  • It allows you to use same name for same task which differs only in parameters. For example in above program you can use different name for each add method but that won't have been a good practice, as all of them are doing the same operation which is addition of numbers.
  • It helps in reducing the overhead of remembering different method name for same task. If we had used different name for each method in above program, we needed to remember four names rather one name.

When we should use method overloading

When you observe that you need to add a method in your program which is going to differ only on the basis of parameters of an existing method, you should use method overloading. In other words if you need to perform two or more same type of task/operation which differs only in parameters, you should use method overloading.

Example of method overloading in pre-defined java classes

There are many java classes where method overloading is used, some of the frequently used classes like String, StringBuffer, StringBuilder etc has many overloaded methods. Methods in String class like indexOf, lastIndexOf, startsWith, substring, toLowerCase, toUpperCase etc are overloaded methods.

Advantages of method overloading in Java

The advantages of using method overloading are :

  • It increases readability and cleanliness of program. Using different name for same behavior or operation reduces readability and understanding of program.
  • It allows you to use common name for two or more than two task which perform same behavior but slightly differ in parameters.
  • It allows you to add new methods with same name in your program, if needed in future.
  • It reduces your overhead to remember multiple names than a single name for same type of task.
★★★
  • Method overloading is an example of compile time polymorphism in java.
  • Call to overloaded method is bonded at compile time itself.
  • Compiler differentiates one method from other method by checking number of parameters or type of parameters. It does not consider return type or access modifier of method while differentiating them.
  • Exception in method declaration also not considered while differentiating two overloaded methods.
  • Each overloaded methods can have same or different return types or access modifiers.
  • Both static and non static methods can be overloaded in java.
  • Methods declared with final keyword can also be overloaded in java.