Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static and default methods in a Java interface?

Differences between static and default methods in Java 8:

1) Default methods can be overriden in implementing class, while static cannot.

2) Static method belongs only to Interface class, so you can only invoke static method on Interface class, not on class implementing this Interface, see:

public interface MyInterface {
    default void defaultMethod(){
        System.out.println("Default");
    }

    static void staticMethod(){
        System.out.println("Static");
    }    
}

public class MyClass implements MyInterface {

    public static void main(String[] args) {

        MyClass.staticMethod(); //not valid - static method may be invoked on containing interface class only
        MyInterface.staticMethod(); //valid
    }
}

3) Both class and interface can have static methods with same names, and neither overrides other!

public class MyClass implements MyInterface {

    public static void main(String[] args) {

        //both are valid and have different behaviour
        MyClass.staticMethod();
        MyInterface.staticMethod();
    }

    static void staticMethod(){
        System.out.println("another static..");
    }
}

A static method is a method that applies to the class 'namespace', so to speak. So a static method foo of interface Interface is accessed by Interface.foo(). Note that the function call does not apply to any particular instance of the interface.

A default implementation bar on the other hand, is called by

Interface x = new ConcreteClass();
x.bar();

A static interface method cannot know about the this variable, but a default implementation can.


1. explain the difference of the two

Static interface methods are like static class methods(here they belong to Interface only). Where as the default interface methods provide default implementation of interface methods (which implementing classes may override)
But remember in case a class is implementing more than one interface with same default method signature then the implementing class needs to override the default method

You can find a simple example below (can DIY for different cases)

public class Test {
    public static void main(String[] args) {
        // Accessing the static member
        I1.hello();

        // Anonymous class Not overriding the default method
        I1 t = new I1() {
            @Override
            public void test() {
                System.out.println("Anonymous test");
            }
        };
        t.test();
        t.hello("uvw");

        // Referring to class instance with overridden default method
        I1 t1 = new Test2();
        t1.test();
        t1.hello("xyz");

    }
}

interface I1 {

    void test();
    //static method
    static void hello() {
        System.out.println("hello from Interface I1");
    }

    // default need not to be implemented by implementing class
    default void hello(String name) {
        System.out.println("Hello " + name);
    }
}

class Test2 implements I1 {
    @Override
    public void test() {
        System.out.println("testing 1234...");
    }

    @Override
    public void hello(String name) {
        System.out.println("bonjour" + name);
    }
}

2. when we would use this would be nice.

That depends on your problem statement. I would say Default methods are useful, if you need same implementation for a method in your specification in all the classes in that contract, Or it may be used like Adapter classes.

here is a good read: https://softwareengineering.stackexchange.com/questions/233053/why-were-default-and-static-methods-added-to-interfaces-in-java-8-when-we-alread

also below oracle doc explains default & static methods for evolving existing interfaces:

Users who have classes that implement interfaces enhanced with new default or static methods do not have to modify or recompile them to accommodate the additional methods.

http://docs.oracle.com/javase/tutorial/java/IandI/nogrow.html


Here is my view:

static method in interface:

  • You can call it directly (InterfacetA.staticMethod())

  • Sub-class will not be able to override.

  • Sub-class may have method with same name as staticMethod

default method in interface:

  • You can not call it directly.

  • Sub-class will be able to override it

Advantage:

  • static Method: You don't need to create separate class for utility method.

  • default Method: Provide the common functionality in default method.


This link has some useful insights, have listed few of them here.

default & static methods have bridged down the differences between interfaces and abstract classes.

Interface default methods:

  • It helps in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.
  • It helps in extending interfaces without having the fear of breaking implementation classes.

Interface static methods:

  • They are part of interface, we can’t use it for implementation class objects.
  • It helps in providing security by not allowing implementation classes to override them.

Like to quote another useful reference.