Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about "super" keyword in this Java example

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

like image 364
okey_on Avatar asked Oct 18 '15 06:10

okey_on


People also ask

What is the use of super keyword in Java with example?

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields. In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default.

What is the difference between this () and super () Keywords?

In java, super keyword is used to access methods of the parent class while this is used to access methods of the current class. this keyword is a reserved keyword in java i.e, we can't use it as an identifier. It is used to refer current class's instance as well as static members.

Can a super () and this () Keywords be in same constructor?

If we include “this()” or “super()” inside the constructor, it must be the first statement inside it. “this()” and “super()” cannot be used inside the same constructor, as both cannot be executed at once (both cannot be the first statement). “this” can be passed as an argument in the method and constructor calls.

Which method Cannot refer to this or super keyword?

Where the "super" keyword in Java is used as a reference to the object of the superclass. This implies that to use "super" the method should be invoked by an object, which static methods are not. Therefore, you cannot use the "super" keyword from a static method.


2 Answers

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
}
like image 88
user3437460 Avatar answered Oct 24 '22 08:10

user3437460


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.

like image 5
janos Avatar answered Oct 24 '22 08:10

janos