-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutDefaultMethods.java
47 lines (36 loc) · 1.23 KB
/
AboutDefaultMethods.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
package java8;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutDefaultMethods {
@Koan
public void interfaceDefaultMethod() {
StringUtil stringUtil = new StringUtil() {
@Override
public String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
};
String capitalizedReversed = stringUtil.capitalize(
stringUtil.reverse("gnirut"));
assertEquals(capitalizedReversed, "TURING");
}
@Koan
public void interfaceStaticMethod() {
assertEquals(StringUtil.enclose("me"), "[me]");
}
interface StringUtil {
//static method in interface
static String enclose(String in) {
return "[" + in + "]";
}
String reverse(String s);
//interface can contain non-abstract method implementations marked by "default" keyword
default String capitalize(String s) {
return s.toUpperCase();
}
default String capitalizeFirst(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
}
}