Why do we need to use the super keyword to invoke default interface methods?

In the code below, when I have a class that implements two interfaces with the same default method signature, ask me to override it. but in overriden mode, why should I use super keyWord to call the default method.

package practice; interface interA{ public default void AImp(){ System.out.println("Calling Aimp from interA"); } } interface interB{ public default void AImp(){ System.out.println("Calling Aimp from interB"); } } public class Practice implements interA,interB { public static void main(String[] args) { Practice inter = new Practice(); inter.AImp(); } @Override public void AImp() { interA.super.AImp(); } } 

I can do the same b using the code below:

 @Override public void AImp() { interA inter = new Practice(); inter.AImp(); } 
+1
source share
3 answers

You did not just redefine; you implemented interfaces with the same named methods. More precisely, you implemented two interfaces, and you need to solve the problem of diamonds , no matter what code you want to run in AImpl. An IDE is required for redefinition because of the hierarchy of diamonds. Once you have provided the code, interA.super is used to navigate to the desired code.

0
source

The practical class implements 2 interfaces. Namely InterA and InterB. The idiom used indicates which of the two default methods you want to call. This is due to the fact that 2 methods have the same signature.

However, when you override the signature in the Practice class as follows:

 package practice; interface InterA { public default void AImp() { System.out.println("Calling Aimp from interA"); } } interface InterB { public default void AImp() { System.out.println("Calling Aimp from interB"); } } public class Practice implements InterA, InterB { public static void main(String[] args) { Practice inter = new Practice(); inter.AImp(); } // @Override // public void AImp() { // // interA.super.AImp(); // } @Override public void AImp() { InterA inter = new Practice(); inter.AImp(); } } 

You will not get the same result. You are getting:

 Exception in thread "main" java.lang.StackOverflowError at practice.Practice.AImp(Practice.java:35) at practice.Practice.AImp(Practice.java:35) at practice.Practice.AImp(Practice.java:35) at practice.Practice.AImp(Practice.java:35) at practice.Practice.AImp(Practice.java:35) at practice.Practice.AImp(Practice.java:35) at practice.Practice.AImp(Practice.java:35) 

You refer to the Practice instance through the InterA interface, which forces you to use the implemented interface, which will again create the instance. Practice and challenge AImp. This will be recursively repeated until java.lang.StackOverflowError occurs.

0
source

Based on comment from @MalteHartwig.

Direct quote from Java Tutorial :

If two or more independent default methods conflict, or the default method conflicts with an abstract method, then the Java compiler generates a compiler error. You must explicitly override the supertype methods.

Consider the example of computer machines that can now fly. You have two interfaces (OperateCar and FlyCar) that provide standard implementations for the same method (startEngine):

 public interface OperateCar { // ... default public int startEngine(EncryptedKey key) { // Implementation } } public interface FlyCar { // ... default public int startEngine(EncryptedKey key) { // Implementation } } 

A class that implements both OperateCar and FlyCar must override the startEngine method. You can call any of the defaults with the super keyword.

 public class FlyingCar implements OperateCar, FlyCar { // ... public int startEngine(EncryptedKey key) { FlyCar.super.startEngine(key); OperateCar.super.startEngine(key); } } 

The name preceding the super (in this example, FlyCar or OperateCar) should refer to the direct superinterface that defines or inherits the default value for the called method. This form of method invocation is not limited to the difference between several implemented interfaces that contain default methods with the same signature. You can use super to call the default method in both classes and interfaces.

0
source

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


All Articles