-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTest.java
More file actions
29 lines (19 loc) · 759 Bytes
/
Test.java
File metadata and controls
29 lines (19 loc) · 759 Bytes
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
public class Test {
private interface Intf { String get(); }
private static class Specific implements Intf { public String get() { return "Specific"; } }
private static class Alternative implements Intf { public String get() { return "Alternative"; } }
public static String caller() {
Alternative a = new Alternative(); // Instantiate this somewhere so there are at least two candidate types in general
return test(new Specific());
}
public static String test(Object o) {
if (o instanceof Intf i) {
// So we should know i.get is really Specific.get():
return i.get();
}
switch (o) {
case Intf i -> { return i.get(); } // Same goes for this `i`
default -> { return "Not an Intf"; }
}
}
}