Latest web development tutorials

Java 8 default method

Java 8 new features Java 8 new features


Java 8 new default interface methods.

Simply put, the default method is the interface can be implemented method and does not require implementation class to implement its methods.

We just in front of the name of the method to add a default keyword to implement the default method.

Why should this feature?

First, before the interface is a double-edged sword, for the benefit of the abstract rather than for specific programming flaw it is that when you need to modify the interface, the need to modify the entire class that implements the interface, the current java collections framework 8 before no foreach method, usually think of solutions in JDK add new methods and implementation to the relevant interface. However, the version has been released, it is not at the same time adding new methods to the interface does not affect the existing implementations. Therefore, the introduction of the default method. Their purpose is to solve the existing modification and implementation of the interface is not compatible with the problem.

grammar

The default method syntax is as follows:

public interface vehicle {
   default void print(){
      System.out.println("我是一辆车!");
   }
}

Multiple default method

Interface has a default method, consider the case, a class implements multiple interfaces, and these interfaces have the same default method, the following examples illustrate the workaround for this situation:

public interface vehicle {
   default void print(){
      System.out.println("我是一辆车!");
   }
}

public interface fourWheeler {
   default void print(){
      System.out.println("我是一辆四轮车!");
   }
}

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

public class car implements vehicle, fourWheeler {
   default void print(){
      System.out.println("我是一辆四轮汽车!");
   }
}

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

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

Static default method

Java 8 Another feature is the interface can declare (and may provide an implementation) static method. E.g:

public interface vehicle {
   default void print(){
      System.out.println("我是一辆车!");
   }
	// 静态方法
   static void blowHorn(){
      System.out.println("按喇叭!!!");
   }
}

The default method of Example

We can use the following code to find out about the default method, you can place the code 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("我是一辆车!");
   }
	
   static void blowHorn(){
      System.out.println("按喇叭!!!");
   }
}

interface FourWheeler {
   default void print(){
      System.out.println("我是一辆四轮车!");
   }
}

class Car implements Vehicle, FourWheeler {
   public void print(){
      Vehicle.super.print();
      FourWheeler.super.print();
      Vehicle.blowHorn();
      System.out.println("我是一辆汽车!");
   }
}

Implementation of the above script, output is:

$ javac Java8Tester.java 
$ java Java8Tester
我是一辆车!
我是一辆四轮车!
按喇叭!!!
我是一辆汽车!

Java 8 new features Java 8 new features