Java8 – Methods in Interfaces

javabullets
1,184 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Java8 – Methods in Interfaces

My previous post on streams demonstrates how useful this feature is to Java8. However it created a problem for the API designers. The problem was how to we extend the existing Collection’s API without breaking existing Collections implementations(guava, apache).

The solution was to allow methods in interfaces, meaning that any implementations already carry an implementation of the extension.

A good example is –

public interface Collection extends Iterable {
    // ....
    default Stream stream() {
        return StreamSupport.stream(spliterator(), false);
    }
    // ....
}

Rules on default methods

  • public
  • default – keyword

Multiple Inheritance

Java always had multiple inheritance in interfaces, but it wasn’t an issue as it didn’t matter which version of the inherited method was implemented.

interface Fred {
    void run();
}
public class OldSkoolMultipleInheritance implements Runnable, Fred {
    @Override
    public void run() {
        System.out.println("Runnable and Fred share the method signature run()");
    }
}

It’s more complex when default methods are involved, and java 8 has an order of precedence –

  • classes > interfaces
  • children > parent
  • else select or override implementation

Examples

interface Interface1 {
    default void defaultMethod() {
        System.out.println("print me!");
    }
}
interface Interface2 {
    default void defaultMethod() {
        System.out.println("no print me!");
    }
}
  • Classes > Interfaces
public class DefaultMethodMultipleInheritance implements Interface1, Interface2 {
    @Override
    public void defaultMethod() {
        System.out.println("Class wins");
    }
}
  • Child Interfaces > Parent Interfaces
interface Interface3 extends Interface2 {
    default void defaultMethod() {
        System.out.println("No no print me!!!");
    }
}
public class DefaultMethodMultipleInheritance implements Interface2, Interface3 {
    public static void main(String[] args) {
        new DefaultMethodMultipleInheritance().defaultMethod();
    }
}
  • Re-implement method in the DefaultMethodsClass and call the specific implementation using super
Interface1.super.newWave(); or Interface2.super.newWave();

eg -

public class DefaultMethodMultipleInheritance implements Interface1, Interface2 {
    @Override
    public void defaultMethod() {
        Interface1.super.defaultMethod();
    }
}

static methods in interfaces

Keep static methods specific to your interface in one location –

  • public
  • static
  • Never inherited

If you have liked this post, check out my personal blog which contains similar tutorials at www.javabullets.com

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content