5) Object-Oriented Programming (OOP) Lesson

Default Interface Methods

6 min to complete · By Ryan Desmond

Introduced in Java 8, default interface methods define a default behavior if the method is not implemented by an implementing class.

Say what?!

Ya, it's a little confusing, but it makes sense once you think about how fragile interfaces can be.

When to Use a Default Method?

Imagine that you work at a company that has a piece of software that they've been developing and using for years. Let's stick with the car company idea. Imagine you've got this MotorizedVehicle interface that is implemented by 50 classes across the application.

But now, all these fancy new electric vehicles that have new functionalities, such as charging, battery levels, etc., are coming out. You want to add these appropriate abstract method signatures to the MotorizedVehicle interface, but if you do, you'll break 50 classes because if you add a new abstract method to an interface, every class that implements that interface must now implement that new charge() method. But that makes no sense because the Tractor is not electric and has no need for a charge() method. 

This is where default interface methods come in. Adding the keyword default to the abstract method signature makes implementing that abstract method optional.

Default Method Example

Here is the MotorizedTransport from before, but with a new default method.

public interface MotorizedTransport {     
    public boolean start();     
    public void stop();     
    public void accelerate(int mph);     
    public void decelerate(int mph); 
    public default charge(double time) {
        System.out.println("Charging not supported");
    }
}

In the interface above, you have defined a new abstract method called charge(double time). But you have declared it as a default method, and you have defined a default behavior that will be executed if this method is called in relation to a class that did not implement the charge method.

This is the one and only exception to an abstract method containing a method body: only default abstract methods can contain a method body.

Keeping with the example, if you're creating a Tesla class, the Tesla class can now implement the MotorizedVehicle interface, including the charge() method.

That said, you do not need to go update the Tractor class and implement the charge() method in there because the Tractor does not need/have/require a charge() method, and you have defined a default behavior for the charge() method. So, in the case of the Tesla class - when the charge() method is invoked, it will call the method and execute the code in the charge() method defined in the Tesla class.

If, for some reason, the charge method is called on a Tractor object (which does not have a charge() functionality and has not implemented the charge() method), then Charging not an option for this vehicle will print.

Summary: What is a Default Method in Java?

  • Default interface methods were introduced in Java 8
  • The default keyword is added to make a method default
  • A default abstract method is the one exception allowing an abstract method to have a method body
  • The method body of a default abstract method will run if the class being invoked does not implement the default abstract method
  • Default interface methods allow you to update interfaces over time without breaking existing code that does not require the update

Syntax

Here's the syntax for creating a default abstract method, where you can substitute the variables starting with your_ with your values.

public interface Your_Interface {     
    public default your_default_abstract_method() {
        //  your default method body
    }
}