-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodReferenceDemo.java
43 lines (35 loc) · 1.11 KB
/
MethodReferenceDemo.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
/**
* Reference : LinkedIn learning. Author : Bethan Palmer
* Advance Java
* Method reference are a short hand way of writing certain type of Lamda Expression.
* For Example : If we pass the variable to a method and do some operation on that variable that can be replaced with
* method reference.
*/
package com.practice.java;
public class MethodReferenceDemo {
public static void main(String[] args) {
// Lamda Expression
Square s = new Square(5);
Shapes sq = (square) -> {
return square.calculateArea();
};
System.out.println("Area of Square is : " + sq.getArea(s));
//Method Reference
Square s1 = new Square(10);
Shapes sq1 = Square::calculateArea;
System.out.println("Area of square by method reference : " + sq1.getArea(s1));
}
}
class Square{
private int sideLength;
public Square(int sideLength) {
this.sideLength = sideLength;
}
public int calculateArea() {
return sideLength * sideLength;
}
}
@FunctionalInterface
interface Shapes {
public abstract int getArea(Square s);
}