C
C
Core Java
Search…
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)
Code
Output
1
package com.test.test1;
2
3
public class LogTest {
4
5
void greet() {
6
System.out.println("Welcome to Home");
7
}
8
9
void greet(String name) {
10
System.out.println("Welcome to Home: " + name);
11
}
12
13
public static void main(String[] args) {
14
LogTest s = new LogTest();
15
s.greet();
16
s.greet("JSM");
17
}
18
19
}
20
Copied!
1
Welcome to Home
2
Welcome to Home: JSM
Copied!
2)
Code
Output
1
package com.test.test1;
2
3
public class Calculate {
4
5
static void area(int side) {
6
int a = side * side;
7
System.out.println("Area of square is" + a);
8
}
9
10
static void area(int length, int breadth) {
11
int a = length * breadth;
12
System.out.println("Area of rectangle is" + a);
13
}
14
15
static void area(double radius) {
16
double d = 3.14 * radius * radius;
17
System.out.println("Area of circle is" + d);
18
}
19
20
public static void main(String[] args) {
21
area(2);
22
area(2, 3);
23
area(3.0);
24
}
25
}
26
Copied!
1
Area of square is4
2
Area of rectangle is6
3
Area of circle is28.259999999999998
Copied!
3)
Code
Output
1
package com.test.test1;
2
3
public class Simple {
4
static void show(int a) {
5
System.out.println("input recieved is: " + a);
6
}
7
8
void show(double d) {
9
System.out.println("input received is: " + d);
10
}
11
12
public static void main(String[] args) {
13
show(10);
14
Simple s2 = new Simple();
15
s2.show(20.5);
16
}
17
18
}
19
Copied!
1
input recieved is: 10
2
input received is: 20.5
Copied!
4)
Code
Output
1
package com.test.test1;
2
3
public class Simple {
4
static void test(int a) {
5
System.out.println("Running test(int a)");
6
}
7
8
static void test(int b) {
9
System.out.println("Running test(int b)");
10
}
11
12
public static void main(String[] args) {
13
test(10);
14
}
15
}
16
Copied!
1
method test(int) is already defined in class
Copied!
5)
Code
Output
1
package com.test.test1;
2
3
public class OverloadingMain {
4
5
public static void main(int a) {
6
7
System.out.println("Main(int a)");
8
9
}
10
11
public static void main(double a) {
12
13
System.out.println("Main(double a)");
14
15
}
16
17
public static void main(String[] args) // Original Main
18
19
{
20
21
main(10);
22
23
main(10.5);
24
}
25
26
}
27
Copied!
1
Main(int a)
2
Main(double a)
Copied!
Last modified 2yr ago
Copy link