-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInterfaceUseWays.java
More file actions
60 lines (51 loc) · 1.1 KB
/
InterfaceUseWays.java
File metadata and controls
60 lines (51 loc) · 1.1 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SAM
@FunctionalInterface
interface Calc{
int compute(int x, int y);
//int compute2(int x, int y);
}
// 1st way
class MyCalc implements Calc{
@Override
public int compute(int x, int y) {
return x + y;
}
}
public class InterfaceUseWays {
public static void main(String[] args) {
// Calc obj = new MyCalc(); // Upcasting
// System.out.println(obj.compute(10, 20));
//System.out.println(new MyCalc().compute(10, 20));
// 2nd Way
// Calc calc = new Calc() {
//
// @Override
// public int compute(int x, int y) {
// // TODO Auto-generated method stub
// return x * y;
// }
//
// };
// System.out.println(calc.compute(10, 2));
//
//
// Calc calc2 = new Calc() {
//
// @Override
// public int compute(int x, int y) {
// // TODO Auto-generated method stub
// return x - y;
// }
//
// };
// System.out.println(calc2.compute(10, 2));
// 3rd Way Lambda
Calc c = (x,y)->x+y;
System.out.println(c.compute(900, 1000));
Calc c2 = (a,b)->{
System.out.println("A is "+a+" And B is "+b);
return a + b;
};
System.out.println(c2.compute(100000, 4324));
}
}