Default methods

suggest change

Introduced in Java 8, default methods put an implementation in an interface. That can replace a base or abstract class when the only goal is a partial implementation, so implementors stay free to extend another class.

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 {
    List<Observer> getObservers();

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

    // notify(String) overloads Object.notify(), which takes no arguments.
    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<Observer> myObservers = new ArrayList<Observer>();

    @Override
    public List<Observer> 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.addObserver(new Observer() {
            @Override
            public void onAction(String a) {
                System.out.println(a + " (" + new Date() + ")");
            }
        });
        w.work();
    }
}

Diamond problem

The Java 8 compiler rejects the diamond problem that appears when a class implements interfaces that contain a method with the same signature.

The class must override the shared method and provide its own implementation. A cast to either interface still runs that override.

interface InterfaceA {
    default String getName() {
        return "a";
    }
}

interface InterfaceB {
    default String getName() {
        return "b";
    }
}

public class ImpClass implements InterfaceA, InterfaceB {
    @Override
    public String getName() {
        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"
    }
}

Two methods with the same name and parameters but different return types still will not compile.

Use default methods to resolve compatibility issues

A default method is useful when a method is added to an interface that several classes already implement.

The default body keeps those classes compiling. Specific classes can override it later.


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