Overloading

Overloading

Two methods are said to be overloaded if and only if the method names are same, But arguments are different (at least Order) Lack of overloading increases complexity of the program in the case of β€˜c’ language.

For the same requirement we should maintain different method names if arguments are 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)
/**
 * 
 * @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 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 immediately compile time error. First it will promote arguments to next level and checks is there any matched method with promoted arguments, if there is no such method compiler will promote the argument to the next level and checks for the matched method. After all possible promotions still the compiler unable to find the matched method then it raises 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.

Overloading


Scroll to top