Method Overloading in Java

Compile-time polymorphism is achieved through method overloading in Java. In simple terms method overloading means there are several methods present inside the class having the same name but different types/order/number of parameters. Sometimes it is referred to as static binding. Because of the Method overloading feature, we can have more than one constructor in a class.

Here is an example of overloading a method by the different number of argument list –

public class JavaExample {

	public JavaExample() {
		// constructor
	}

	public void add(int a, int b) {
		int result = a + b;
		System.out.println("add(int,int) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(int a, int b, int c) {
		int result = a + b + c;
		System.out.println("add(int,int, int) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(int a, int b, int c, int d) {
		int result = a + b + c + d;
		System.out.println("add(int,int,int,int) method called!");
		System.out.println("after addition - " + result);

	}

	public static void main(String[] args) {

		JavaExample ex = new JavaExample();
		ex.add(4, 5);
		ex.add(4, 5, 6);
		ex.add(4, 5, 6, 7);
	}
}
Example Output

Here is an example of overloading a method by different data types –

public class JavaExample {

	public JavaExample() {
		// constructor
	}

	public void add(int a, int b) {
		int result = a + b;
		System.out.println("add(int,int) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(int a, double b) {
		double result = a + b;
		System.out.println("add(int,double) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(double a, int b) {
		double result = a + b;
		System.out.println("add(double,int) method called!");
		System.out.println("after addition - " + result);

	}

	public void add(String a, String b) {
		String result = a + b;
		System.out.println("add(String ,String ) method called!");
		System.out.println("after addition - " + result);
	}

	public static void main(String[] args) {

		JavaExample ex = new JavaExample();
		ex.add(4, 5);
		ex.add(4.0, 5);
		ex.add("Java", " Programming");
		ex.add(5, 5.50);
	}
}
Example Output

Here is an example of overloading a method by a different order of arguments –

public class JavaExample {

	public JavaExample() {
		// constructor
	}

	public void add(int a, double b) {
		double result = a + b;
		System.out.println("add(int,double) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(double a, int b) {
		double result = a + b;
		System.out.println("add(double, int) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(String a, char b) {
		String result = a + b;
		System.out.println("add(String, char) method called!");
		System.out.println("after addition - " + result);
	}

	public void add(char a, String b) {
		String result = a + b;
		System.out.println("add(char, String) method called!");
		System.out.println("after addition - " + result);
	}

	public static void main(String[] args) {

		JavaExample ex = new JavaExample();
		ex.add(4, 5.5);
		ex.add(5.5, 4);
		ex.add("India ", 'Y');
		ex.add('Y', " India");
	}

}
Example Output

Share