Overloading

Overloading

In this tutorial, we are going to discuss Overloading in Java. Method overloading is a feature in many programming languages that allows a class to have multiple methods with the same name but with different parameter lists.

Two methods are said to be overloaded if the method names are the same, But arguments are different (at least Order). Lack of overloading increases the complexity of the program in the case of the C language.

Overloading in Java

For the same requirement, we should maintain different method names if the arguments are the same.

In C language

abs(int i) -> only for int arguments.
fabs(float f) -> only for float arguments.
labs(long l) -> only for long arguments.

But in java, two methods with the same name is allowed even though arguments are different.

abs(int i)
abs(float f)
abs(long l)

Here’s a simple example in Java

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public void m1() {
      System.out.println("no-args");
   }

   public void m1(int i) {
      System.out.println("int-args");
   }

   public void m2(double d) {
      System.out.println("double-args");
   }

   public static void main(String[] args) {
      Test t = new Test();
      t.m1();
      t.m1(10);
      t.m1(10.5);
   }
}

The overloading method resolution is the responsibility of the compiler based on reference type and method arguments. Hence overloading is considered as compile-time polymorphism or Early Binding.

Automatic promotion in overloading
/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public void m1() {
      System.out.println("no-args");
   }

   public void m1(int i) {
      System.out.println("int-args");
   }

   public void m1(float f) {
      System.out.println("float-args");
   }

   public static void main(String[] args) {
      Test t = new Test();
      t.m1();
      t.m1(10);
      t.m1(10l);
      t.m1(10f);
      t.m1('a');
      t.m1(10.5);
   }
}

In the case of overloading, if there is no method with the required argument, then the compiler won’t raise compile-time error immediately. First, it will promote arguments to the next level and check is there any matched method with promoted arguments. If there is no such method compiler will promote the argument to the next level and check for the matched method. After all possible promotions, the compiler is still unable to find the matched method, which raises a compile-time error.

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public void m1(String s) {
      System.out.println("String Version");
   }

   public void m1(Object o) {
      System.out.println("Object Version");
   }

   public static void main(String arg[]) {
      Test t = new Test();
      t.m1("raju"); -> String Version
      t.m1(new Object()); -> Object Version
      t.m1(null);
   }
}

In the case of overloading, the more specific version will get the chance first.

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public void m1(String s) {
      System.out.println("String Version");
   }

   public void m1(StringBuffer sb) {
      System.out.println("StringVersion");
   }

   public static void main(String arg[]) {
      Test t = new Test();
      t.m1("raju");
      t.m1(null);
   }
}

So it produces Ambiguity error.

Rules for Overloading in Java

Below are the rules that should remember in java overloading:

  • The first and foremost rule of Method overloading is that Methods need to have the same name in a single class.
  • Two or more methods in a class can be overloaded based on different signatures, and signature includes the number of parameters, data types of parameters, the sequence of parameters as explained above.
  • The return type of a method is not a part of the signature, so overloading can never be done based on the return type, and if done, this creates the compile-time error.

Overloading can make your code more readable and intuitive by allowing you to use the same method name for similar operations with different data types.

That’s all about Overloading in Java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!

Overloading
Scroll to top