Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1481 :

Posted: Fri May 03, 2019 6:51 am
by zel_bl

Code: Select all

Even in case of interfaces, a subinterface cannot override a default method with a static method.

Code: Select all

You can, however, have a default method in a subinterface with the same signature as a static method of its super interface because a static method of an interface can only be called using that interface's name.
If you could explain this more closely.
Isn't the static method in the first case non overriding method if its static, and it can only be called using the interface name too.
Thanks

Re: About Question enthuware.ocajp.i.v8.2.1481 :

Posted: Fri May 03, 2019 6:59 am
by admin

Code: Select all

interface I{
default void dm(){ }
static void sm(){ }
}

interface SubI extends I{
static void dm(){ } //won't compile, error: dm() in SubI clashes with dm() in I
default void sm(){ }  //valid 
}
As far as why this rule exists, only the Java designers can tell. My guess is that if you have a static method in the subinterface, how will you then override the default method from the super interface in the sub interface if you wanted to?

Re: About Question enthuware.ocajp.i.v8.2.1481 :

Posted: Mon Mar 28, 2022 10:53 am
by jrzabott
This question does not seem OK.
I mean, using Azul JDK 1.8 and IntelliJ an attempts to compile question's code fail.

Code: Select all

package mypackage;

interface iii {
    default String getS() {
        return "111";
    }
}

interface myInt extends iii {
    String value = "AValue";

    default String getS() {
        return "";
    }

    String getS();
}

public class TestClass {
    public static void main(String[] args) {
        TestClass t = new TestClass();
    }
}
I always get this message from compiler:

Code: Select all

/Users/djunior/IdeaProjects/untitled/src/mypackage/TestClass.java:16:12
java: method getS() is already defined in interface mypackage.myInt

Re: About Question enthuware.ocajp.i.v8.2.1481 :

Posted: Mon Mar 28, 2022 11:42 am
by admin
You need to use the official Oracle JDK and use its command line tools to compile your code because that is what the exam is based on.
You might want to check this article as well regarding usage of IDEs while preparing for the exam: https://enthuware.com/oca-ocp-java-cert ... cation-ide