Default Method Declaration

Java-Default Method

Default Method

Recently, a feature was added to interface that makes a significant change to its capabilities. Prior to JDK 8, an interface could not define any implementation whatsoever. This meant that for all previous versions of Java, the methods specified by an interface were abstract, containing no body. This is the traditional form of an interface. The release of JDK 8 changed this by adding a new capability to interface called the default method. A default method lets you define a default implementation for an interface method. In other words, by use of a default method, it is possible for an interface method to provide a body, rather than being abstract.

A primary motivation for the default method was to provide a means by which interfaces could be expanded without breaking existing code. Recall that there must be implementations for all methods defined by an interface. In the past, if a new method were added to a popular, widely used interface, then the addition of that method would break existing code because no implementation would be found for that new method. The default method solves this problem by supplying an implementation that will be used if no other implementation is explicitly provided. Thus, the addition of a default method will not cause preexisting code to break.

Another motivation for the default method was the desire to specify methods in an interface that are, essentially, optional, depending on how the interface is used. For example, an interface might define a method that act on a speed of vehicle. These method might be called topSpeed( ), and its purpose is to show the top speed of the vehicle. However, if the interface is intended to support both modifiable and nonmodifiable sequences, then topSpeed( ) is essentially optional because it won’t be used by nonmodifiable sequences. In the past, a class that implemented a nonmodifiable sequence would have had to define an empty implementation of topSpeed( ), even though it was not needed. Today, a default implementation for topSpeed( ) can be specified in the interface that does nothing (or throws an exception). Providing this default prevents a class used for nonmodifiable sequences from having to define its own, placeholder version of topSpeed( ). Thus, by providing a default, the interface makes the implementation of topSpeed( ) by a class optional.

Default Method Declaration

An interface default method is defined similar to the way a method is defined by a class. The primary difference is that the declaration is preceded by the keyword default. For example :

Default Method Declaration
Interface SpeedAndName declares two methods. The first, getName( ), is a standard interface method declaration. It defines no implementation whatsoever. The second method is topSpeed( ), and it does include a default implementation. In this case, it simply print the string “Speed Undefined”. Its declaration is preceded by the default modifier. This syntax can be generalized. To define a default method, precede its declaration with default. Because topSpeed( ) includes a default implementation, it is not necessary for an implementing class to override it. In other words, if an implementing class does not provide its own implementation, the default is used. For example, the BMW class shown next is perfectly valid :

Default Method Declaration
The following code creates an instance of BMW and uses it to call both getName( ) and topSpeed( ).

Default Method Declaration

The default implementation of topSpeed( ) was automatically used. It was not necessary for BMW to define it. Thus, for topSpeed( ), implementation by a class is optional. It is both possible and common for an implementing class to define its own implementation of a default method. For example, BMW overrides topSpeed( ) :

Default Method Declaration
Program

Implementing-InterfaceDefault Method Declaration

Program Source

interface Speed {
    
    default void topSpeed() {
        
        System.out.println("Speed Undefined");
    }
}
class Audi implements Speed {
    
    public void topSpeed() {
        
        System.out.println("Audi : 250 km/h");
    }
}
class Ford implements Speed {

    public void topSpeed() {
        
        System.out.println("Ford : 220 km/h");
    }
}
class BMW implements Speed {
    
}
public class Javaapp {

    public static void main(String[] args) {
        
        Audi audi = new Audi();
        audi.topSpeed();
        Ford ford = new Ford();
        ford.topSpeed();
        BMW  bmw  = new BMW();
        bmw.topSpeed();
    }
}

Leave a Comment