Page 1 of 1

About Question enthuware.ocpjp.v8.2.1481 :

Posted: Mon Mar 07, 2016 5:08 pm
by javalass
You cannot override a static method with a non-static method and vice-versa.
This is not quite true when it comes to interfaces (which is the case here). Static methods in interfaces are never inherited, so declaring a non-static method with the same signature as a static method from a superinterface is possible. This, for example, is fine:

Code: Select all

interface Readable {
    static String getAction() {
        return "read";
    }
}

interface ReadableInEnglish extends Readable {
    default String getAction() {
        return "read in English";
    }
}
and so is this:

Code: Select all

interface Readable {
    static String getAction() {
        return "read";
    }
}

interface ReadableInEnglish extends Readable {
    String getAction();    
}
but NOT these, since default methods ARE inherited:

Code: Select all

interface Readable {
    default String getAction() {
        return "read";
    }
}

interface ReadableInEnglish extends Readable {
    static String getAction() { //won't compile, can't override default method with static method
        return "read in English";
    }
}

Code: Select all

interface Readable {
    String getAction();
}

interface ReadableInEnglish extends Readable {
    static String getAction() { //won't compile, can't override abstract method with static method
        return "read in English";
    }
}
The answer to the question is correct though, this is just a note about the explanation provided.

Re: About Question enthuware.ocpjp.v8.2.1481 :

Posted: Mon Mar 07, 2016 8:55 pm
by admin
You are right. Fixed.
thank you for your feedback!
Paul.