Skip to content

Commit 206def8

Browse files
committed
Add an example showing how to work with unrealted default methods
1 parent 1030082 commit 206def8

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.codecentric.java8examples.defaultmethods;
2+
3+
/**
4+
* A greeting service implementation that inherits {@link #greet()} from two unrelated interfaces. It has to provide
5+
* an implementation for {@code greet()}.
6+
*/
7+
public class CombinedGreetingService implements GreetingService, AlternativeGreetingService {
8+
9+
/**
10+
* An implementation of the {@code greet()} method which is defined in both, {@link GreetingService} and
11+
* {@link AlternativeGreetingService}. This implementation simply delegates to the default {@code greet()}
12+
* implementation of the {@code GreetingService} interface
13+
*
14+
* @return the result of calling {@link GreetingService#greet()}.
15+
*/
16+
@Override
17+
public String greet() {
18+
return GreetingService.super.greet();
19+
}
20+
}

src/test/java/de/codecentric/java8examples/defaultmethods/GreetingServiceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ public void greetFromExtended() throws Exception {
2323
public void greetFromDerived() throws Exception {
2424
assertEquals("Salut le monde!", new DerivedGreetingService().greet());
2525
}
26+
27+
@Test
28+
public void testName() throws Exception {
29+
assertEquals("Hello World!", new CombinedGreetingService().greet());
30+
31+
}
2632
}

0 commit comments

Comments
 (0)