Home » Java » Method Overloading in Java

Method Overloading in Java

What is Method Overloading?

Overloading in java occurs when methods in a same class or in child classes shares same name but different parameters or arguments.Method Parameters can be changed by following way.

  • Have different number of arguments.
  • Have same number of arguments but their types are different.
  • Have both different numbers of arguments with a difference in their types.
  • Sequence of Data type of parameters.

Note:- Return type of method will not play any roles in method overloading.

1. Example of Method Overloading by changing the number of arguments :

In this example, we have created two overloaded methods, first sum method performs addition of two numbers and second sum method performs addition of three numbers.


package com.example;

public class OverloadingEx {
	void sum(int a, int b) {
		int sum = a + b;
		System.out.println("sum of no is " + sum);

	}

	void sum(int a, int b, int c) {
		int sum = a + b + c;
		System.out.println("sum of no is " + sum);
	}

	public static void main(String args[]) {
		OverloadingEx obj = new OverloadingEx();
		obj.sum(5, 10, 10);
		obj.sum(10, 10);

	}
}

Output

sum of no is 25
sum of no is 20

2. Example of Method Overloading by changing data type of argument

In this example, we have created two overloaded methods that have different data types in argument. The first sum method receives two integer arguments and second sum method receives two double arguments.


package com.example;

public class OverloadingEx {
	void sum(int a, int b) {
		int sum = a + b;
		System.out.println("sum of no is " + sum);

	}

	void sum(double a, double b) {
		double sum = a + b;
		System.out.println("sum of no is " + sum);
	}

	public static void main(String args[]) {
		OverloadingEx obj = new OverloadingEx();
		obj.sum(5, 10);
		obj.sum(5.0, 2.0);

	}
}

Output

sum of no is 15
sum of no is 7.0

3. Example of Method Overloading by changing sequence of data type of argument

Here method sum() is overloaded based on sequence of data type of arguments – Both the methods have different sequence of data type in argument list. First method is having argument list as (int, double) and second having (double, int). Since the sequence is different, the method can be overloaded without any problem.


package com.example;

public class OverloadingEx {
	void sum(int a, double b) {
		int sum = (int) (a + b);
		System.out.println("sum of no is " + sum);

	}

	void sum(double b, int a) {
		double sum = a + b;
		System.out.println("sum of no is " + sum);
	}

	public static void main(String args[]) {
		OverloadingEx obj = new OverloadingEx();
		obj.sum(5, 10.0);
		obj.sum(5.0, 2);

	}
}

Output

sum of no is 15
sum of no is 7.0

Method Overloading is not possible by changing the return type of method

In java, method overloading is not possible by changing the return type of the method.Consider the class given below. When you will call sum() method how can java determine which sum() method should be called and it will result compilation error.


package com.example;

public class OverloadingEx {
	int sum(int a, int b) { //compilation Error
		return a + b;
	}

	double sum(int a, int b) {
		return a + b;
	}

	public static void main(String args[]) { //compilation Error
		OverloadingEx obj = new OverloadingEx();
		obj.sum(5, 5);

	}
}

Method overloading in subclass

It is also possible to overload methods in sub classes, as long as the methods shares same name and they are different in the number of parameters or type of parameters.


public class Animal {
   public void run(int distance){
      ......
   }
}

public class Dog extends Animal {

   public void run(int distance) {
      ......  
   }
   public void run(int distance, String place) {
      ......
   }

}

Example Valid/invalid method overloading :

Example 1 :


int sum(int a, int b, float c)
int sum(int var1, int var2, float var3)

Invalid case of method overloading.Above code will not compile because argument are exactly same. Both methods have same number of data types and same sequence of data types in arguments.


Example 2 :


int sum(int a, int b)
int sum(float var1, float var2)

Valid case of method overloading. Here data types of arguments are different.


Example 3 :


int sum(int a, int b)
int sum(int a)

Valid case for overloading. Here number of arguments are different.


Example 4 :


float sum(int a, float b)
float sum(float var1, int var2)

Valid case of method overloading. Sequence of the data types are different, first method is having (int, float) and second is having (float, int).


Example 5 :


int sum(int a, int b)
float sum(int var1, int var2)

Invalid case of method overloading. Argument lists are exactly same.Return type of method doesn’t matter while overloading a method.


Method Overloading and Type promotion In Java :

Type promotion is an automatic type conversion from a "lesser" base type to a "greater" one.When resolving overloaded methods if exact method is not available then we do not get any compile time error immediately, the compiler first promotes the arguments to the next level and checks whether the matched method is available, if its available then compiler considers it, otherwise promotes it to next level. This process will continue until all possible promotions are completed. If no match is found we would get compiler error.

  • One type is promoted to another implicitly if no matching datatype is found.
  • byte can be promoted to short, int, long, float or double.
  • The short datatype can be promoted to int,long,float or double.
  • The char datatype can be promoted to int,long,float or double etc.

Example of Method Overloading with TypePromotion


package com.example;

public class TypePromotion {

	void add(float a, float b) {
		System.out.println(a + b + " float");
	}

	void add(double a, double b) {
		System.out.println(a + b + " double");
	}

	public static void main(String args[]) {
		TypePromotion obj = new TypePromotion();
		obj.add(21, 21); // integer value is passed
	}
}

In the above example integer value is passed through obj.add(21,21) which will be implicitly promoted to its nearest bigger type i.e. float type. [note:nearest bigger type of int is long which is not matched in the above program, therefore next bigger type is matched and is promoted to float implicitly]

Output

42.0 float

Example of method overloading with Type Promotion giving ambiguity


package com.example;

public class TypePromotion {

	void add(int a, long b) {
		System.out.println(" 1st method invoked");
	}

	void add(long a, int b) {
		System.out.println(" 2nd method invoked");
	}

	public static void main(String args[]) {
		TypePromotion obj = new TypePromotion();
		obj.add(21, 21);
	}
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method add(int, long) is ambiguous for the type TypePromotion

	at com.example.TypePromotion.main(TypePromotion.java:15)

In the above code Compile time error will be generated because one-type cannot be de promoted to any type implicitly.

Previous Next Article

comments powered by Disqus