on Leave a Comment

Java Method Overloading with Examples

In java, we can create different methods having same name but different parameters list and definition. This concept is called method overloading. Method overloading is useful when objects are required to perform similar task but having different parameters. For example, suppose you have to perform addition of numbers, then you write a method first(int a, int b) to add two number, then again you write a method second(int a, int b, int c) to add three numbers and so on. Now problem is, it is difficult to remember methods name for adding numbers. Method overloading allows similar methods having same name but having different number of parameters.

Ways to Overload Methods

- Having Different numbers of parameter.
- Parameters having different data type.
- Sequence of different data type parameters in parameter list.

Method overloading is way to achieve static polymorphism.

Examples of Methods Overloading

1. Having Different numbers of parameter.

class Example
{
 public int add(int a, int b)
 {
  return a+b;
 }
 public int add(int a, int b, int c)
 {
  return a+b+c;
 }
 public int add(int a, int b, int c, int d)
 {
  return a+b+c+d;
 }
}

class MethodOverloading
{
 public static void main(String[] args) {
  Example obj = new Example();

  System.out.println("Sum of 1 and 2 is "+obj.add(1, 2));
  System.out.println("Sum of 1, 2 and 3 is "+obj.add(1, 2, 3));
  System.out.println("Sum of 1, 2, 3 and 4 is "+obj.add(1, 2, 3, 4));
 }
}

Output:

Sum of 1 and 2 is 3
Sum of 1, 2 and 3 is 6
Sum of 1, 2, 3 and 4 is 10

2. Parameters having different data type.

class Example
{
 public int add(int a, int b)
 {
  return a+b;
 }
 public float add(float a, float b)
 {
  return a+b;
 }
}

class MethodOverloading
{
 public static void main(String[] args) {
  Example obj = new Example();

  System.out.println("Sum of 1 and 2 is "+obj.add(1, 2));
  System.out.println("Sum of 1.1 and 2.2 is "+obj.add(1.1f, 2.2f));
 }
}

Output:

Sum of 1 and 2 is 3
Sum of 1.1 and 2.2 is 3.3000002

3. Sequence of different data type parameters in parameter list.

class Example
{
 public float add(int a, float b)
 {
  return a+b;
 }
 public float add(float a, int b)
 {
  return a+b;
 }
}

class MethodOverloading
{
 public static void main(String[] args) {
  Example obj = new Example();

  System.out.println("Sum of 1 and 2.2 is "+obj.add(1, 2.2f));
  System.out.println("Sum of 1.1 and 2 is "+obj.add(1.1f, 2));
 }
}

Output:

Sum of 1 and 2.2 is 3.2
Sum of 1.1 and 2 is 3.1

Note: Return type doesn't matter in method overloading.

Can we overload java main() method?

Like other method, java main() method can also be overload.

But JVM always calls main methods having string array as parameter.

Example:

class MethodOverloading
{
 public static void main(String[] args) {
  System.out.println("Main method with String[] args");
 }
 public static void main(String args) {
  System.out.println("Main method with String args");
 }
 public static void main() {
  System.out.println("Main without parameter");
 }
}

Output:

Main method with String[] args


Method Overloading Valid/Invalid Cases

Invalid Cases

Case 1:

int add(int a, int b, int c)
int add(int x, int y, int z)

Invalid, because both method having same number of parameters with same data type and having same sequence.

Case 2:

int add(int a, int b, int c)
float add(int x, int y, int z)

Invalid, because both method having same number of parameters with same data type and having same sequence.

Valid Cases

Case 1:

int add(int a, int b)
int add(int a, int b, int c)

Valid, because both method have different number of parameters.

Case 2:

int add(int a, int b, int c)
float add(float a, float b)

Valid, because both method having different parameter type.

Case 3:

float add(int a, float b)
float add(float a, int b)

Valid, because both method having different sequence of parameters.

0 comments:

Post a Comment