-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInterfaceUseWays.java
More file actions
71 lines (41 loc) · 1.05 KB
/
InterfaceUseWays.java
File metadata and controls
71 lines (41 loc) · 1.05 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
61
62
63
64
65
66
67
68
69
70
71
// SAM (Single Abstract Method) Interface
@FunctionalInterface
interface Calc{
//void add();
int calculate(int x, int y);
}
// 1st way of using interface
// Step -1 Create a Class which implements the interface
class MyCalc implements Calc
{
// Step-2 Override the interface method
@Override
public int calculate(int x, int y){
return x + y;
}
}
public class InterfaceUseWays {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Step-3 Create Object in Upcasting Style
//Calc calc = new MyCalc();
//System.out.println(calc.calculate(100, 200));
// Anonymous Class
// Internally
// class ___ implements Calc
/*Calc calc= new Calc(){
@Override
public int calculate(int x, int y){
return x * y;
}
};
System.out.println("Calc is "+calc.calculate(100, 200));
*/
// Lambda Expression (Java 8)
Calc c = (a,b)->{
int d = 0;
return a+b+d;
};
System.out.println("Lambda Expression "+c.calculate(1000, 2000));
}
}