# 14) Method Overloading

### Real Life Scenario:

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTf22wKU0-3ZdG7Z6fe%2F-LTf2F1TXiC8BR5uIrAG%2Fimage.png?alt=media\&token=67935930-5a44-4196-948c-af4b1d019d60)

![](https://3494582050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LSemf7mp1liQa7nfIAC%2F-LTf22wKU0-3ZdG7Z6fe%2F-LTf2JqfRaEnQNGZysAn%2Fimage.png?alt=media\&token=a088b40d-74f0-4f02-a37a-8fabd4787f3e)



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

&#x20;Variations in the arguments list can be:-&#x20;

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.&#x20;
2. It is easy to remember method name.&#x20;
3. We can achieve efficiency in the program readability.
4. We can achieve compile-time Polymorphism through Overloading.&#x20;
5. &#x20;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.&#x20;
6. We can overload static and non-static methods.

**Interview Questions:**

1\) Can we overload main method?&#x20;

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.&#x20;

2\) Can we overload Non-static method?&#x20;

Yes.

3\) Can the return type differ while overloading?&#x20;

Yes.

**Examples:**

**1)**

{% tabs %}
{% tab title="Code" %}
```java
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");
	}

}

```
{% endtab %}

{% tab title="Output" %}
```
Welcome to Home
Welcome to Home: JSM
```
{% endtab %}
{% endtabs %}

**2)**

{% tabs %}
{% tab title="Code" %}
```java
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);
	}
}

```
{% endtab %}

{% tab title="Output" %}
```
Area of square is4
Area of rectangle is6
Area of circle is28.259999999999998
```
{% endtab %}
{% endtabs %}

**3)**

{% tabs %}
{% tab title="Code" %}
```java
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);
	}

}

```
{% endtab %}

{% tab title="Output" %}
```
input recieved is: 10
input received is: 20.5
```
{% endtab %}
{% endtabs %}

**4)**

{% tabs %}
{% tab title="Code" %}
```java
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);
	}
}

```
{% endtab %}

{% tab title="Output" %}
```
method test(int) is already defined in class
```
{% endtab %}
{% endtabs %}

**5)**

{% tabs %}
{% tab title="Code" %}
```java
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);
	}

}

```
{% endtab %}

{% tab title="Output" %}
```
Main(int a)
Main(double a)
```
{% endtab %}
{% endtabs %}

