File tree Expand file tree Collapse file tree
main/java/de/codecentric/java8examples/defaultmethods
test/java/de/codecentric/java8examples Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 55 */
66public interface GreetingService {
77
8+ /**
9+ * Creates a greeting message. The provided default implementation simply returns "Hello world!"
10+ *
11+ * @return A greeting message.
12+ */
813 default String greet () {
914 return "Hello World!" ;
1015 }
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change @@ -60,6 +60,8 @@ public void peterIsOlderThan30WithTypeInference() throws Exception {
6060 public void getAgeFromWrappedElementViaFunctionApplication () throws Exception {
6161 // type is inferred from context
6262 assertEquals ("Parker" , example .apply (p -> p .getLastName ()));
63+ // different notation using a method reference
64+ assertEquals ("Parker" , example .apply (Person ::getLastName ));
6365 }
6466
6567 @ Test
You can’t perform that action at this time.
0 commit comments