Vaguely about the "super" key in this Java example

In this example, the training website has a java page . Two interfaces define the same default method startEngine() . The FlyingCar class implements both interfaces and must override startEngine() due to an obvious conflict.

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

I donโ€™t understand why from FlyingCar , super a link to both versions of startEngine() in the OperateCar and FlyCar . As far as I understand, startEngine() not defined in any superclass, therefore it should not be referred to as resident in one. I also do not see any connection between super and two interfaces implemented in FlyingCar

+5
source share
2 answers

As I understand it, startEngine () was not defined in any superclass, therefore it should not be referred to as resident in one.

Yes, that was determined. This is the default implementation, for example:

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

OperateCar.super.startEngine(key) will execute the default implementation.

If there was no default implementation, just an interface method, then the statement does not make sense, since the interface will not contain an implementation, for example:

 public interface OperateCar { // ... int startEngine(EncryptedKey key); } 

I also do not see any connection between the super and two interfaces implemented in FlyingCar

Not sure if I understand what you are asking. super is a way to invoke an implementation in the parent interface. Without super there is no other way to express it.

+5
source

If you have a class that implements several interfaces, and these interfaces contain methods with a similar method signature (for example, your startEngine method).

To find out which method you are calling, follow these steps:

 yourInterface.super.method(); 

You can see the link, which will also solve your question.

So you can also do this:

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

Or that:

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

In any case, you need to specify the interface from which you are calling the method, if you are just calling the method from the interface.

However, this particular situation is an example of a cause. What will be more likely is that you will do something like this:

 public int startEngine(EncryptedKey key) { // Custom implemenation... } 

This is also true. However, if you have two interfaces with a method that has an identical signature, it could also be a situation where you must have one parent interface that declares this method, and two child interfaces that extend it:

 public interface Car { // ... default public int startEngine(EncryptedKey key) { // Default implementation }; } public interface FlyCar extends Car { // Methods unique to FlyCar } public interface OperateCar extends Car { // methods unique to OperateCar } 
+5
source

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


All Articles