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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

jrzabott
Posts: 3
Joined: Mon Mar 28, 2022 10:49 am
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 66 guests