REMEMBER JAVA

MINTE9
2REMEMBER




DEFAULT

p102 ! Prior to Java 8, adding methods to interfaces breaks existing implemenations. Default method construct was added to avoid this.
 CodeCopy
class App {

    public static void main(String[] args) {

        new LoginCmd();
        new SubscribeCmd();
    }
}

interface Configurable {
    public abstract void getConfig();
}

interface Commandable {
    public default void execute() {} // Look Here
}

class SubscribeCmd implements Configurable, Commandable {
    @Override public void getConfig() {}
    public void execute() {}
}

/**
 * Old class - doesn't imnplement execute()
 */
class LoginCmd implements Configurable, Commandable { 
    @Override public void getConfig() {}
}
... 22 lines
˄˄˄
p104 Using default for existing interfaces should be avoided unless the need is critical. It must be used with great care, without forgeting that an interface is a contract.
 CodeCopy
class App {

    public static void main(String[] args) {

        new Command_1();
        new Command_2(); // no execute()
    }
}

interface Commandable {
    public void execute();
    public void getResponse();
}

interface CommandableDefault extends Commandable {
    public default void execute() {} // Look Here
}

class Command_1 implements Commandable {
    @Override public void execute() {}
    @Override public void getResponse() {}
}

class Command_2 implements CommandableDefault { // Look Here
    @Override public void getResponse() {}
}
... 20 lines
˄˄˄

Questions    
No. 60   Import Static
CARD
59
63

        A B C D E F
🔔
1/1