diff --git a/src/main/java/de/codecentric/java8examples/defaultmethods/CombinedGreetingService.java b/src/main/java/de/codecentric/java8examples/defaultmethods/CombinedGreetingService.java new file mode 100644 index 0000000..f8db072 --- /dev/null +++ b/src/main/java/de/codecentric/java8examples/defaultmethods/CombinedGreetingService.java @@ -0,0 +1,20 @@ +package de.codecentric.java8examples.defaultmethods; + +/** + * A greeting service implementation that inherits {@link #greet()} from two unrelated interfaces. It has to provide + * an implementation for {@code greet()}. + */ +public class CombinedGreetingService implements GreetingService, AlternativeGreetingService { + + /** + * An implementation of the {@code greet()} method which is defined in both, {@link GreetingService} and + * {@link AlternativeGreetingService}. This implementation simply delegates to the default {@code greet()} + * implementation of the {@code GreetingService} interface + * + * @return the result of calling {@link GreetingService#greet()}. + */ + @Override + public String greet() { + return GreetingService.super.greet(); + } +} diff --git a/src/main/java/de/codecentric/java8examples/defaultmethods/GreetingService.java b/src/main/java/de/codecentric/java8examples/defaultmethods/GreetingService.java index 16e4c6d..473dbd8 100644 --- a/src/main/java/de/codecentric/java8examples/defaultmethods/GreetingService.java +++ b/src/main/java/de/codecentric/java8examples/defaultmethods/GreetingService.java @@ -5,6 +5,11 @@ */ public interface GreetingService { + /** + * Creates a greeting message. The provided default implementation simply returns "Hello world!" + * + * @return A greeting message. + */ default String greet() { return "Hello World!"; } diff --git a/src/test/java/de/codecentric/java8examples/defaultmethods/GreetingServiceTest.java b/src/test/java/de/codecentric/java8examples/defaultmethods/GreetingServiceTest.java index 7e3d72e..81a5ee1 100644 --- a/src/test/java/de/codecentric/java8examples/defaultmethods/GreetingServiceTest.java +++ b/src/test/java/de/codecentric/java8examples/defaultmethods/GreetingServiceTest.java @@ -23,4 +23,10 @@ public void greetFromExtended() throws Exception { public void greetFromDerived() throws Exception { assertEquals("Salut le monde!", new DerivedGreetingService().greet()); } + + @Test + public void testName() throws Exception { + assertEquals("Hello World!", new CombinedGreetingService().greet()); + + } } diff --git a/src/test/java/de/codecentric/java8examples/lambdas/LambdaExampleTest.java b/src/test/java/de/codecentric/java8examples/lambdas/LambdaExampleTest.java index 3feb748..3d7dd1d 100644 --- a/src/test/java/de/codecentric/java8examples/lambdas/LambdaExampleTest.java +++ b/src/test/java/de/codecentric/java8examples/lambdas/LambdaExampleTest.java @@ -60,6 +60,8 @@ public void peterIsOlderThan30WithTypeInference() throws Exception { public void getAgeFromWrappedElementViaFunctionApplication() throws Exception { // type is inferred from context assertEquals("Parker", example.apply(p -> p.getLastName())); + // different notation using a method reference + assertEquals("Parker", example.apply(Person::getLastName)); } @Test