Confused about “super” keyword in this Java example

↘锁芯ラ 提交于 2019-12-06 01:47:32

问题


In this example on java website's tutorial page. Two interfaces define the same default method startEngine(). A class FlyingCar implements both interfaces and must override startEngine() because of the 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 is used to refer to both versions of startEngine() in OperateCar and FlyCar interfaces. As I understand it, startEngine() was not defined in any super class, therefore shouldn't be referred as resident in one. I also do not see any relationship between super and the two interfaces as implemented in FlyingCar


回答1:


As I understand it, startEngine() was not defined in any super class, therefore shouldn't be referred as resident in one.

Yes it was defined. It's 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 wouldn't make sense, as the interface wouldn't contain an implementation, like this:

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

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

Not sure I understand what you're asking. super is a way to call the implementation in the parent interface. Without super, there's just no other way to express that.




回答2:


When you have a class implementing multiple interfaces, and those interfaces contains methods with similar method signature (for e.g. your startEngine method).

In order to know which method you are referring to, you do:

yourInterface.super.method();

You can take a look at this link which also addresses your question.

So, you could also do this:

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

Or this:

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

Either way, you have to specify the interface you are calling the method from, if you are simply calling the method from the interface.

However, this particular situation is an example for a reason. What will be more likely to happen is that you will be doing something like this:

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

Which is also valid. However, if you have two interfaces with a method that has an identical signature, that might also be a situation where you should have a single parent interface that declares that 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
}


来源:https://stackoverflow.com/questions/33195189/confused-about-super-keyword-in-this-java-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!