Default methods

suggest change

Introduced in Java 8, default methods are a way of specifying an implementation inside an interface. This could be used to avoid the typical “Base” or “Abstract” class by providing a partial implementation of an interface, and restricting the subclasses hierarchy.

Observer pattern implementation

For example, it’s possible to implement the Observer-Listener pattern directly into the interface, providing more flexibility to the implementing classes.

interface Observer {
    void onAction(String a);
}

interface Observable{

public abstract List getObservers();

public default void addObserver(Observer o){ getObservers().add(o); }

public default void notify(String something ){ for( Observer l : getObservers() ){ l.onAction(something); } }

}

Now, any class can be made “Observable” just by implementing the Observable interface, while being free to be part of a different class hierarchy.

abstract class Worker{
    public abstract void work();
}

public class MyWorker extends Worker implements Observable {

private List myObservers = new ArrayList();

@Override public List getObservers() { return myObservers; }

@Override public void work(){ notify(“Started work”);

// Code goes here...

notify("Completed work");

}

public static void main(String[] args) {

MyWorker w = new MyWorker();

w.addListener(new Observer() {
    @Override
    public void onAction(String a) {
        System.out.println(a + " (" + new Date() + ")");
    }
});

w.work();

}

}

Diamond problem

The compiler in Java 8 is aware of the diamond problem which is caused when a class is implementing interfaces containing a method with the same signature.

In order to solve it, an implementing class must override the shared method and provide its own implementation.

interface InterfaceA {

public default String getName(){ return “a”; }

}

interface InterfaceB {

public default String getName(){ return “b”; }

}

public class ImpClass implements InterfaceA, InterfaceB {

@Override public String getName() {

//Must provide its own implementation return InterfaceA.super.getName() + InterfaceB.super.getName(); }

public static void main(String[] args) {

ImpClass c = new ImpClass();

System.out.println( c.getName() );                   // Prints "ab"
System.out.println( ((InterfaceA)c).getName() );     // Prints "ab"
System.out.println( ((InterfaceB)c).getName() );     // Prints "ab"

}

}

There’s still the issue of having methods with the same name and parameters with different return types, which will not compile.

Use default methods to resolve compatibility issues

The default method implementations come in very handy if a method is added to an interface in an existing system where the interfaces is used by several classes.

To avoid breaking up the entire system, you can provide a default method implementation when you add a method to an interface. This way, the system will still compile and the actual implementations can be done step by step.


For more information, see the Default Methods topic.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents