New feature of Java 1.8-interface default method

New feature of Java 1.8-interface default method

In the past, there were often interview questions asking whether specific methods could be written in the interface, but now there are still some rookies who will come up with this interview question. Java 8 adds the default method of the interface. Simply put, the default method is that the interface can have implementation methods, and no implementation class is required to implement its methods. We only need to add a default keyword in front of the method name to implement the default method.

Why is there such a feature? The interface is a double-edged sword. The advantage is that it is oriented to abstraction rather than specific programming. The disadvantage is that when the interface needs to be modified, all classes that implement the interface need to be modified. To solve this problem, the default method introduced is to solve the modification of the interface Incompatibility with existing implementations. When you need to modify the interface, you can directly add silent methods without modifying all implementation classes.

syntax

The default method syntax format is as follows:

public interface vehicle {
   default void print(){
      System.out.println("I am a car!");
   }
}

Multiple default methods

An interface has a default method. Considering this situation, a class implements multiple interfaces, and these interfaces have the same default method. The following example illustrates 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-wheeled vehicle!");
   }
}

The first solution is to create your own default method to override the default method of overriding the interface:

public class car implements vehicle, fourWheeler {
   default void print(){
      System.out.println("I am a four-wheeled car!");
   }
}

The second solution can use super to call the default method of the specified interface:

public class car implements vehicle, fourWheeler {
   default void print(){
      vehicle.super.print();
   }
}

Static default method

Another feature of Java 8 is that interfaces can declare (and provide implementations) static methods. E.g:

public interface vehicle {
   default void print(){
      System.out.println("I am a car!");
   }
 //static method
   static void blowHorn(){
      System.out.println("Hon the horn!!!");
   }
}

Default method instance

We can learn about the use of the default method through the following code, which can be put into the 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("Hon the horn!!!");
   }
}

interface FourWheeler {
   default void print(){
      System.out.println("I am a four-wheeled vehicle!");
   }
}

class Car implements Vehicle, FourWheeler {
   public void print(){
      Vehicle.super.print();
      FourWheeler.super.print();
      Vehicle.blowHorn();
      System.out.println("I am a car!");
   }
}

Execute the above script, the output result is:

$ javac Java8Tester.java 
$ java Java8Tester
I am a car!
I am a four-wheeler!
Honk the horn!!!
I am a car!