Java 8 default method

Java 8 added a default method for the interface.
In simple terms, the default method is that the interface can have implementation methods, and does not need to implement the class to achieve its method.
We only need to add the default keyword in front of the method name to implement the default method.
Why do you have this feature?
First of all, the previous interface is a double-edged sword. The advantage is that it is oriented to abstraction rather than to specific programming. The drawback is that when the interface needs to be modified, all the classes that implement the interface need to be modified. The current collection framework before java 8 does not have a foreach method. The usual solution is to add new methods and implementations to the relevant interfaces in the JDK. However, for the released version, it is impossible to add new methods to the interface without affecting the existing implementation. So introduce the default method. Their purpose is to solve the problem that the modification of the interface is incompatible with the existing implementation.

grammar

The default method syntax is as follows:
public interface vehicle { default void print(){
System.out.println( " I'm a car! " ) ; }
}

Multiple default methods

An interface has default methods. Consider a situation where a class implements multiple interfaces and these interfaces have the same default methods. The following example illustrates the solution to this situation:
public interface vehicle { default void print(){ System.out.println("I'm a car! "); } } public interface fourWheeler { default void print(){ System.out.println("I'm a four. Wheeler!"); } }
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("I'm a four-wheeler!"); } }
The second solution can use super to call the default method of the specified interface:
public class car implements vehicle, fourWheeler { public 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'm a car!"); } // static method static void blowHorn(){ System.out.println("Hello!!!"); } }

Default method instance

We can use the following code to understand the use of the default method, you can put the code into 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("I'm a horn!!!");
} } interface FourWheeler { default void print(){ System.out.println("I'm a four-wheeler!"); } } class Car implements Vehicle, FourWheeler { public void print(){ Vehicle.super.print(); FourWheeler.super.print(); Vehicle.blowHorn(); System.out.println("I'm a car!"); } }
Execute the above script, the output is:
$ javac Java8Tester.java 
$ java Java8Tester
I'm a car! 
I'm a four-wheeler ! 
I'm a horn! 
I'm a car!

Comments