Interface: Default Methods Tutorial

Prior to Java 8, the biggest issue with interfaces was a complete lack of scalability. Scalability allows an application to evolve and grow over time without breaking existing code. It is a concept that is learned mostly through experience. With a pre-Java 8 interface you had just about one chance to get your interface right and that was it. Once your interface was in production it became almost impossible to make a change or addition that didn't have major ripple effects.
In my Introduction to Interfaces tutorial I created two separate classes that implemented the same Engine interface, a Boeing787 and a Honda Accord. Let's suppose that I wanted to add a new abstract method called getHorsePower(), what would happen to the implementing classes? The next time I compiled both the 787 and the Accord classes, I would receive an error about an abstract method that I did not override. If I wanted to compile my code, and I do, I will have no choice but to override the new method. In the real world – if my interface was distributed and in production – I would have a bunch of very angry programmers knocking on my door.
The default method solves this issue and provides us with a new capability that did not exist before. Unlike an abstract method, a default method has a method body and it can execute statements and return values. A default method is similar to a regular class method only with the default keyword applied. I'm thinking that an interface is starting to look much more like an abstract class now ... interesting.
Rules that apply to a default method:

  • A default method must be explicitly declared default. Not to be confused with default (package-private) access.
  • A default method is implicitly public.
  • A default method may have only the following legal modifiers: public, default, and strictfp.
  • A default method may be overridden in the implementing class.
  • A default method does not have to be overridden in the implementing class.
  • If you override the default method in an implementing class, you can still invoke the interface method by using this syntax: InterfaceName.super.defaultMethod().


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md DefaultMethodTest
C:\Java>cd DefaultMethodTest
C:\Java\DefaultMethodTest>Notepad DefaultMethodTest.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class DefaultMethodTest {

    public static void main(String args[]) {

        HondaAccord accord = new HondaAccord("un-leaded gasoline");
        System.out.println("The engine of a Honda Accord burns " + accord.getFuelType());
        System.out.println("The Honda Accord is designed to safely carry " + accord.getMaxPassengers() + " passengers");
        //System.out.println("The Honda Accord has " + accord.getHorsePower("sedan") + " horsepower");
    }
}

interface Engine {
    String getFuelType();

    //int getHorsePower(String modelType); // Error - break all implementing classes

    //default int getHorsePower(String modelType) {
    //    return -1; // My intention really is for the implementing class to override this method
    //}
}

interface Passengers {
    int getMaxPassengers();    
}

class HondaAccord implements Engine, Passengers {
    String fuelType = "";
    static final int MAX_PASSENGERS = 5;
    HondaAccord() { super(); }
    HondaAccord(String fuelType) {
        this.fuelType = fuelType;
    }
    @Override
    public String getFuelType() {
        return fuelType;
    }
    @Override
    public int getMaxPassengers() {
        return MAX_PASSENGERS;
    }
    //@Override
    //public int getHorsePower(String modelType) {
    //    if (modelType.equals("sedan")) { 
    //        return 177; 
    //    }
    //    return Engine.super.getHorsePower(modelType);
    //}
    
}

Now switch back to the command prompt (CMD) and type in javac DefaultMethodTest.java and press Enter.
Now type in java DefaultMethodTest and press Enter.


C:\Java\DefaultMethodTest>javac DefaultMethodTest.java
C:\Java\DefaultMethodTest>java DefaultMethodTest
The engine of a Honda Accord burns un-leaded gasoline
The Honda Accord is designed to safely carry 5 passengers
The Honda Accord has 177 horsepower.


Final thoughts

The default method sort of solves the scalability issue with interfaces. You won't break any existing code, but you can't force the implementing class to override the new method either. One new feature of the default method is that when you are designing an interface you can now include optional method implementation. The new functionality also begins to grey the line between an abstract class and an interface.


Tutorials