Friday, 31 May 2013

Method Overloading

Method Overloading: If class have multiple methods by same name but different types of parameters or different number of parameters it is known as "Method Overloading".
Rules to Overload methods:
  1. Method should appear in the same class or a subclass
  2. Method should have the same name but,
  3. have different parameter lists, or,
  4. can have different return types for parameters.
NOTE: Method Overloading is used to achieve compile time polymorphism.

Following example shows how to overload the method:

 *************************************************

public class MethodOverloadingExample {

  public void addition(int a, int b) {
     int c = a + b;
     System.out.println("addition of two integres" + c);
  }

  public void addition(float a, float b) {
     float c = a + b;
     System.out.println("addition of two float numbers" + c);
  }

 public void addition(double a, double b, double c) {
    double d = a + b + c;
    System.out.println("addition of three double numbers" + d);
 }

 public static void main(String a[]) {
    MethodOverloadingExample example = new MethodOverloadingExample();
    // calls the addition of floating numbers method
    example.addition(10.0F, 20.0F);
    // calls the addition of double numbers method
    example.addition(10.0, 20.0, 30.0);
    // calls the addition of integres numbers method
    example.addition(50, 60);
 }
}


 *************************************************


In this example three methods have same name like addition but based on number/return type of parameters we can call those methods. So here addition() is overloaded method.

Can we Overload main() method:
Yes by method overloading. We can have any number of main methods in class by method overloading. 

Let see following example:

 *************************************************


public class MainMethodOverloadExample {

 public static void main(int a) {

    System.out.println("overloaded main method" + a);
 }

 public static void main(String a[]) {

    System.out.println("main method");
    main(10);
 }

}

 *************************************************

NOTE: In Java method overloading is not possible by changing return type of method.

Why method overloading is not possible by changing the return type of method?
Because here may occur ambiguity. 
Let see following example how ambiguity may occur:

 *************************************************

public class MethodAmbiguityExample {

public int add(int a, int b) {

System.out.println("sum of numbers is " + (a + b));
return a + b;
}

public double add(int a, int b) {
System.out.println("sum of numbers is " + (a + b));
return a + b;
}

public static void main(String a[]) {
MethodAmbiguityExample ambiguityExample = new 
                 MethodAmbiguityExample();
ambiguityExample.add(10, 10);
}

}

 *************************************************

Above example show error like "Duplicate method add(int, int) in type MethodAmbiguityExample". to fix this we need to rename one add() method to some other name. So overloading is not possible by changing return type of method.

No comments:

Post a Comment