In java 8, why cannot call the interface static method that the current class is implementing -
this question has answer here:
i'm playing around java 8's new features , observed interesting behaviour:
this okay:
class { static void staticmethodina() {println();} } class b extends {} b.staticmethodina();
this induce error of: static method may invoked on containing interface class only.
interface { static void staticmethodina() {println();} } class b implements {} b.staticmethodina(); // here intellij complaints..
can tell me why designer of java 8 may choose treat above 2 cases differently?
addition of static
methods in interface in java 8 came 1 restriction - methods cannot inherited class implementing it. , makes sense, class can implement multiple interface. , if 2 interfaces have same static
method, both inherited, , compiler wouldn't know 1 invoke.
however, extending class, that's no issue. static
class methods inherited subclass.
see jls §8.4.8:
a class c inherits direct superclass concrete methods m (both static , instance) of superclass
...
a class c inherits direct superclass , direct superinterfaces abstract , default (§9.4) methods m
...
a class does not inherit static methods superinterfaces.
Comments
Post a Comment