Java implements two interfaces and resolves default conflicts by default: why use a super keyword?

Java resolves default conflicts by default: why use a super keyword?

I am reading the book "Core Java SE 9 for the impatient." In the "Resolving Default Method Conflicts" section, I found that when resolving default method conflicts, the super keyword is used. But I do not understand why use the super keyword.

With this link: https://docs.oracle.com/javase/tutorial/java/IandI/override.html I know that the super keyword is required (otherwise the program will not compile).

But that seems contradictory. In my opinion, the Identified interface does not have a superclass. Why not just use " return Identified.getId(); "?

 public interface Person { default int getId() { return 0; } } public interface Identified { default int getId() { return Math.abs(hashCode()); } } public class Employee implements Person, Identified { public int getId() { return Identified.super.getId(); } } 

There are several explanations and question in this question , but none of them explained why the super keyword is used.

+5
source share
3 answers

Because Identified.getId() means getId is a static method. Prior to Java 8, this super keyword was used only to denote a superclass in the form super.getId() .

In your case, Identified.super.getId() does not mean "GetId () is a super identifier", but rather, "The getId () is super which is from an identifier."

+3
source

Clearly, Employee has a conflict; the signature of the default int getId() method of both Person and Identified identical.

JLS 9.4.1.3 describes this as a behavioral conflict that leads to an error. Naturally, you have to decide which getId() method should be called (either override it, or choose the default implementation from Person or Identified .

JLS 9.4.1.1 (Overriding by instance methods) gives the key:

Access to the default overridden method can be obtained using the call expression method (Β§15.12), which contains the super keyword with the name of the superinterface.

But the most formalized reason for using the word super comes from JLS 15.12 and, in particular, 15.12.1: Compilation time Step 1. Defining a class or interface to search

The section describes 6 forms that a method call can take.

One form of Identified.getId() reserved for a static link and is discussed in another answer.

Another form of super.getId() . This is a more common use of the word super ; which refers to the superclass of the class.

This leads to the final form mentioned in 12.15.1: TypeName.super.getId()

Here the word super has only one purpose. If the word super appears to the left of the getId () method, and if TypeName appears to the left of the super, then (i) TypeName must be either a class or an interface, (ii) If TypeName is a class, it takes precedence over the interface, (iii) If TypeName is not a class; it must be an interface.

So, this ultimately leads to an explanation of super in this context:

Super in this case is used to match the TypeName.super.identifier form , which is a certain way (according to JLS 15.12.1), to call the method identifier from the TypeName interface.

+2
source

Because the syntax of Indentified.getId() already used for calling static methods. The C ++ Identifier::getId() syntax might be nice, but Java uses it to refer to a method in the same way as non-go. The syntax Identified.this.getId() cannot be used either because it has already been used for the avec Γ  method from an external class when you have nested classes.

I think at some point they just ran out of options. But I agree that the syntax can be misleading, but since you cannot name the method "grandparent" in Java, using "super" could be considered a lesser evil.

+1
source

Source: https://habr.com/ru/post/1275671/


All Articles