Java Reference
In-Depth Information
You've seen that default methods are useful for many usage patterns. But here's some food for
thought: what if a class implements two interfaces that have the same default method signature?
Which method is the class allowed to use? We explore this problem in the next section.
9.4. Resolution rules
As you know, in Java a class can extend only one parent class but implement multiple interfaces.
With the introduction of default methods in Java 8, there's the possibility of a class inheriting
more than one method with the same signature. Which version of the method should be used?
Such conflicts will probably be quite rare in practice, but when they do occur there must be rules
that specify how to deal with the conflict. This section explains how the Java compiler resolves
such potential conflicts. We aim to answer questions such as “In the code that follows, which
hello method is C calling?” Note that the examples that follow are intended to explore
problematic scenarios; it doesn't mean such scenarios will happen frequently in practice:
In addition, you may have heard of the diamond problem in C++ where a class can inherit two
methods with the same signature. Which one gets chosen? Java 8 provides resolution rules to
solve this issue too. Read on!
9.4.1. Three resolution rules to know
There are three rules to follow when a class inherits a method with the same signature from
multiple places (such as another class or interface):
1 . Classes always win. A method declaration in the class or a superclass takes priority over any
default method declaration.
 
Search WWH ::




Custom Search