-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodOverloading.java
More file actions
38 lines (29 loc) · 1.15 KB
/
Copy pathMethodOverloading.java
File metadata and controls
38 lines (29 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class MethodOverloading {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Overloaded method to add three numbers
// Same method name but different number of parameters
public int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
// Create an object of MethodOverloading class
MethodOverloading mo = new MethodOverloading();
// Call the add() method with three arguments
// Java automatically selects the matching method
int sum = mo.add(10, 20, 30);
// Print the result
System.out.println(sum);
}
// POINTS REMEMBER :
// 1. Method name same but type of parameters,
// number of parameters and
// order of parameters must be different.
// 2. Return type alone can not overload the method.
// 3. Static and not-static methods can be overloaded.
// 4. Constrcutor can be overoaded but diffrent parameters.
// 5. Its compile time polymorphism because compiler decides
// which methods to call during compilation based on the argument passed.
}