-
Notifications
You must be signed in to change notification settings - Fork 1
/
Overloading.java
63 lines (50 loc) · 1.86 KB
/
Overloading.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Understanding Overloading
// Overloading is a concept in Java where a class can have multiple methods with the same name but different parameters.
// Method or Constructor overloading is a static/compile-time polymorphism.
// The methods must have the same name but different parameters.
// The return type of the methods can be different.
// The methods can be overloaded in the same class or in a subclass.
// The methods can have different access modifiers.
// The methods can throw different exceptions.
// The methods can have different return types.
// The methods can have different number of parameters.
class Calculation {
String operations;
Calculation() {
this.operations = "Add Sub";
}
Calculation(String operations) {
this.operations = operations;
}
public int addition(int a, int b) {
return a + b;
}
public int addition(int a, int b, int c) {
return a + b + c;
}
public void addition(float a, float b) {
System.out.println("Addition: " + (a + b));
return ;
}
public int subtraction(int a, int b) {
return a - b;
}
public int subtraction(int a, int b, int c) {
return a - b - c;
}
public float subtraction(float a, float b) {
return (a - b);
}
}
public class Overloading {
public static void main(String[] args) {
Calculation calc = new Calculation();
System.out.println("Operations: " + calc.operations);
System.out.println("Addition: " + calc.addition(10, 20));
System.out.println("Addition: " + calc.addition(10, 20, 30));
calc.addition(10.5f, 20.5f);
System.out.println("Subtraction: " + calc.subtraction(10, 20));
System.out.println("Subtraction: " + calc.subtraction(10, 20, 30));
System.out.println("Subtraction: " + calc.subtraction(10.5f, 20.5f));
}
}