Java 8 Default Methods
Java 8 introduced default methods for interfaces.
Simply put, default methods allow interfaces to have implemented methods without requiring the implementing class to implement those methods.
We just need to add the default
keyword in front of the method name to create a default method.
>
Why is this feature needed?
Previously, interfaces were a double-edged sword; they promoted abstraction over concrete programming, but the drawback was that modifying an interface required modifying all classes that implemented it. Before Java 8, the collection framework lacked a foreach
method. The usual solution was to add new methods and their implementations to the relevant interfaces in the JDK. However, for released versions, it was impossible to add new methods to interfaces without affecting existing implementations. Hence, default methods were introduced to address the issue of interface modifications causing incompatibilities with existing implementations.
Syntax
The syntax for default methods is as follows:
public interface Vehicle {
default void print(){
System.out.println("I am a car!");
}
}
Multiple Default Methods
An interface can have default methods. Consider a scenario where a class implements multiple interfaces that have the same default method. The following example demonstrates the solution to this situation:
public interface Vehicle {
default void print(){
System.out.println("I am a car!");
}
}
public interface FourWheeler {
default void print(){
System.out.println("I am a four-wheeler!");
}
}
The first solution is to create your own default method to override the interface's default method:
public class Car implements Vehicle, FourWheeler {
default void print(){
System.out.println("I am a four-wheeled car!");
}
}
The second solution is to use super
to call the default method of a specified interface:
public class Car implements Vehicle, FourWheeler {
public void print(){
Vehicle.super.print();
}
}
Static Default Methods
Another feature of Java 8 is that interfaces can declare (and provide implementations for) static methods. For example:
public interface Vehicle {
default void print(){
System.out.println("I am a car!");
}
// Static method
static void blowHorn(){
System.out.println("Pressing the horn!!!");
}
}
Default Method Example
We can understand the use of default methods through the following code, which can be placed in the Java8Tester.java
file:
Java8Tester.java File
public class Java8Tester {
public static void main(String args[]){
Vehicle vehicle = new Car();
vehicle.print();
}
}
interface Vehicle {
default void print(){
System.out.println("I am a car!");
}
static void blowHorn(){
System.out.println("Pressing the horn!!!");
}
}
interface FourWheeler {
default void print(){
System.out.println("I am a four-wheeler!");
}
}
class Car implements Vehicle, FourWheeler {
public void print(){
Vehicle.super.print();
FourWheeler.super.print();
Vehicle.blowHorn();
System.out.println("I am a car!");
}
}
Executing the above script will produce the following output:
$ javac Java8Tester.java
$ java Java8Tester
I am a car!
I am a four-wheeler!
Pressing the horn!!!
I am a car!