Wednesday, 1 February 2017

METHOD OVERLOADING IN JAVA

Process of defining multiple methods with the same name but with different parameters types or list or order is called method overloading. To execute same logic with different type of arguments we should overload methods. For example to add two integers, two floats, two strings we should define three methods with same name.

Example –
public class Overloading {
          void add(int a, int b)
          {
                   System.out.println("Result = "+(a+b));
          }
          void add(float a, float b)
          {
                   System.out.println("Result = "+(a+b));
          }
          void add(String a,String b)
          {
                   System.out.println("Result = "+(a+b));
          }
          public static void main(String[] args) {
                   Overloading o = new Overloading();
                   o.add("rakesh", "kumar");
          }
}

Output –
Result = rakeshkumar

No comments:

Post a Comment