algorithm for method overloading in java code example

Example 1: program for method overloading in java

class Calculate
{
  void sum (int a, int b)
  {
    System.out.println("sum is"+(a+b)) ;
  }
  void sum (float a, float b)
  {
    System.out.println("sum is"+(a+b));
  }
  Public static void main (String[] args)
  {
    Calculate  cal = new Calculate();
    cal.sum (8,5);      //sum(int a, int b) is method is called.
    cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
  }
}

Example 2: method overloading

Method overloading is providing two separate methods in a class 
with the same name but different arguments, while the method return type 
may or may not be different, which allows us to reuse the same method name.

Tags:

Misc Example