@jlacar/

Functional Interfaces and casting (Example 2)

Java

Shows a class that implements multiple functional interfaces. Unlike the FI, implementations are not limited to a single method.

fork
loading
Files
  • Main.java
Main.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
public class Main {
  public static void main(String[] args) {
    Foo f = () -> System.out.println("Foo!");
    f.foo();
    
    Bar b = () -> System.out.println("Bar!");
    b.bar();
    
    Glom g = new Glom();
    
    ((Foo) g).foo();
    ((Bar) g).bar();
  }
  
}

interface Foo {
  void foo();
}

interface Bar {
  void bar();
}

class Glom implements Foo, Bar {
  @Override
  public void foo() {
    System.out.println("Glom Foo!");
  }
  
  @Override
  public void bar() {
    System.out.println("Glom Bar!");
  }
}