14) Method Overloading

Real Life Scenario:

Definition: Developing multiple methods with the same name in same class, but variations in the arguments list.

Variations in the arguments list can be:-

  1. Number of arguments.

  2. Types of arguments.

  3. Position/order of arguments

Whenever we want to perform the common operation or task with the variations in the inputs, we should choose Method Overloading.

Example:

If you want to write a program to perform addition of 2 numbers and 3 numbers, then we should develop 2 methods with 1 to receive 2 inputs and 1 more to receive 3 inputs. Instead of developing these methods with different names, we can develop with same name “add”.

Example:

If you are developing an application and you are giving 2 options for the user name and password and another with the mobile numbers and password. Here also we should develop 2 methods with the variation in the inputs instead of developing this methods with different name the operation is common, we can develop it with the same name.

Note: Through Method Overloading:-

  1. We can achieve consistency in the method names, which are developed for common purpose.

  2. It is easy to remember method name.

  3. We can achieve efficiency in the program readability.

  4. We can achieve compile-time Polymorphism through Overloading.

  5. While overloading, method name should be same with the variations in the arguments list, other parts of method does not matter, i.e., Return type, modifiers, access level can be different.

  6. We can overload static and non-static methods.

Interview Questions:

1) Can we overload main method?

Yes, we can but the program execution starts with main method having arguments as (String[] args) Other versions of the main program should be invoked by the programmer explicitly.

2) Can we overload Non-static method?

Yes.

3) Can the return type differ while overloading?

Yes.

Examples:

1)

package com.test.test1;

public class LogTest {

	void greet() {
		System.out.println("Welcome to Home");
	}

	void greet(String name) {
		System.out.println("Welcome to Home: " + name);
	}

	public static void main(String[] args) {
		LogTest s = new LogTest();
		s.greet();
		s.greet("JSM");
	}

}

2)

package com.test.test1;

public class Calculate {

	static void area(int side) {
		int a = side * side;
		System.out.println("Area of square is" + a);
	}

	static void area(int length, int breadth) {
		int a = length * breadth;
		System.out.println("Area of rectangle is" + a);
	}

	static void area(double radius) {
		double d = 3.14 * radius * radius;
		System.out.println("Area of circle is" + d);
	}

	public static void main(String[] args) {
		area(2);
		area(2, 3);
		area(3.0);
	}
}

3)

package com.test.test1;

public class Simple {
	static void show(int a) {
		System.out.println("input recieved is: " + a);
	}

	void show(double d) {
		System.out.println("input received is: " + d);
	}

	public static void main(String[] args) {
		show(10);
		Simple s2 = new Simple();
		s2.show(20.5);
	}

}

4)

package com.test.test1;

public class Simple {
	static void test(int a) {
		System.out.println("Running test(int a)");
	}

	static void test(int b) {
		System.out.println("Running test(int b)");
	}

	public static void main(String[] args) {
		test(10);
	}
}

5)

package com.test.test1;

public class OverloadingMain {

	public static void main(int a) {

		System.out.println("Main(int a)");

	}

	public static void main(double a) {

		System.out.println("Main(double a)");

	}

	public static void main(String[] args) // Original Main

	{

		main(10);

		main(10.5);
	}

}

Last updated