Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cannot use this while calling static interface method from default interface method? [duplicate]

Tags:

Why when in interface, calling static interface method from default interface method I cannot use this.staticInterfaceMethod(), while when in regular class, calling static class method from instance method it is perfectly valid to use this.staticClassMethod() (though it is bad style)?

At the same time, it is perfectly valid to use this within default methods of interface - I can legally do the following:

interface I {   
    int MY_CONST = 7;   
    static void st_f() {}       
    default void f1() {}

    default void f_demo() {
        this.f1();                  // fine!
        int locvar = this.MY_CONST; // also fine! 

        this.st_f(); // c.ERR:  static method of interface I can only be accessed as I.st_f 
    }
}
like image 958
Code Complete Avatar asked Feb 01 '18 20:02

Code Complete


People also ask

Why static method is not allowed in interface?

This is because it's not allowed in java, since Object is the base class for all the classes and we can't have one class level static method and another instance method with same signature.

Can we override static and default methods in interface?

Static method in interface are part of the interface class can't implement or override it whereas class can override the default method.

Can we have 2 default method in interface?

Multiple Defaults With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods.

Why do we need default and static methods in interface?

Java 8 introduced default and static methods in interfaces. This feature enables us to add new functionality in the interfaces without breaking the existing contract of the implementing classes.


1 Answers

"Static method may be invoked on containing interface class only."

Static methods are not inherited from an interface by the class that implements the interface (§8.4.8). The this keyword refers to the current object, hence attempting to invoke this.staticInterfaceMethod() would imply that somehow an Object exists that has inherited the static interface method. This cannot happen; interfaces cannot be instantiated by themselves and any implementation of the interface did not inherit the static method. Thus, this.staticInterfaceMethod() does not exist so you would need to invoke the method on the Interface itself.

A simplified explanation as to why you don't inherit the static method is the following scenario:

public interface InterfaceA {
    public static void staticInterfaceMethod() {
        ...
    }
}

public interface InterfaceB {
    public static void staticInterfaceMethod() {
        ...
    }
}

public class ClassAB implements InterfaceA, InterfaceB {
    ...
}

What static method would ClassAB inherit? The two static methods have the same signature and thus would be unidentifiable on a call; neither would hide the other.

At the same time, it is perfectly valid to use this within default methods of interface.

Using the this keyword to reference default methods, variables and etc is permitted as every Object that can exist that inherits from the interface will inherit these properties.

like image 73
Zachary Avatar answered Sep 23 '22 12:09

Zachary