Static Method in Interface

Java emphasises Separation of Concern a lot. Cohesion allows putting the same functionality or type of method in 1 place. Each Interface adds some new functionality and requirement. hence there can be some functionality methods specific to the Interface. but since Java 7 doesn't allow method definition but allows abstract method declaration only. all the implementing classes had to implement the Interface specific method.

As the Interface grows old and has a lot of implemented classes, it is very difficult to introduce a new method in the Interface and override all implementing classes. so it was not possible to have a design where each Interface can contribute a specific functionality.

package org.wesome.dsalgo.java8;

interface Apple {
    //  functionality provided by Apple Interface
    void likedApple(String fruitName);
}

interface Mango {
    //  functionality provided by Apple Interface
    void likedMango(String vegetableName);
}

class Fruit implements Apple, Mango {
    @Override
    public void likedApple(String fruitName) {
        System.out.println("i love Apple, my favourite apple is " + fruitName);
    }

    @Override
    public void likedMango(String vegetableName) {
        System.out.println("i love Mango, my favourite mango is " + vegetableName);
    }

    public static void main(String[] args) {
        Fruit fruit = new Fruit();
        fruit.likedApple("Fuji");
        fruit.likedMango("Alphonso");
    }
}

Java 8 introduced the concept of the static method. just like a normal class static methods, static methods defined in Interface is part of and are bound with Interface only, hence cannot be overridden by implementing class. Just like a normal class static methods, static method defined in Interface will be called via Interface name. The static method contains a complete implementation of the functionality. These are resolved at compile time.

Default methods can be overridden but the static methods cannot

the static method of the interface cannot be overridden hence functionalities defined in an interface cannot be modified by any implementing class. it provides security and avoids poor implementation in the subclass as well.

package org.wesome.dsalgo.java8;

interface Apple {
    static void likedApple(String fruitName) {
        System.out.println("i love Apple, my favourite apple is " + fruitName);
    }
}

interface Mango {
    static void likedMango(String vegetableName) {
        System.out.println("i love Mango, my favourite mango is " + vegetableName);
    }
}

class Fruit implements Apple, Mango {
    public static void main(String[] args) {
        Apple.likedApple("Fuji");
        Mango.likedMango("Alphonso");
    }
}

A static method can be called from a normal instance method, another static method and from another default method as well.

package org.wesome.dsalgo.java8;

interface Apple {
    static void likedApple(String fruitName) {
        System.out.println("i love Apple, my favourite apple is " + fruitName);
    }
}

interface Mango {
    static void likedMango(String vegetableName) {
        System.out.println("i love Mango, my favourite mango is " + vegetableName);
        /*  static method called from another static method    */
        Apple.likedApple("Fuji");
    }

    default void likedApple(String fruitName) {
        System.out.println("i love Apple, my favourite apple is " + fruitName);
        /*  static method called from default method    */
        Apple.likedApple("Fuji");
    }
}

class Fruit implements Apple, Mango {
    public static void main(String[] args) {
        /*  static method called directly from main method    */
        Mango.likedMango("Alphonso");
        Mango fruit = new Fruit();
        fruit.likedApple("Fuji");
    }
}

follow us on