Java Reference
In-Depth Information
2 . Otherwise, sub-interfaces win: the method with the same signature in the most specific
default-providing interface is selected. (If B extends A, B is more specific than A).
3 . Finally, if the choice is still ambiguous, the class inheriting from multiple interfaces has to
explicitly select which default method implementation to use by overriding it and calling the
desired method explicitly.
We promise, these are the only rules you need to know! Let's now look at some examples.
9.4.2. Most specific default-providing interface wins
Let's revisit our example from the beginning of this section where C implements both B and A,
which define a default method called hello. In addition, B extends A. Figure 9.5 provides a UML
diagram for the scenario.
Figure 9.5. The most specific default-providing interface wins.
Which declaration of the hello method will the compiler use? Rule 2 says that the method with
the most specific default-providing interface is selected. Because B is more specific than A, the
hello from B is selected. Consequently the program will print “Hello from B.”
Now, consider what would happen if C were inheriting from D as follows (illustrated in figure
9.6 ):
Figure 9.6. Inheriting from a class and implementing two interfaces
 
Search WWH ::




Custom Search