English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java 8 New default methods have been added to the interfaces.
In simple terms, default methods are methods that interfaces can have implementations, and the implementing class does not need to implement the method.
We can implement default methods by simply adding the default keyword before the method name.
Why do we need this feature?
Firstly, the previous interfaces were a double-edged sword. The advantage was to program towards abstraction rather than towards specifics, and the disadvantage was that when the interface needed to be modified, all classes that implemented the interface had to be modified. In the current java 8 The previous collection framework did not have a foreach method, and the usual solution would be to add new methods and implementations to the interfaces in JDK. However, for already released versions, it is not possible to add new methods to the interfaces without affecting the existing implementations. Therefore, default methods were introduced. Their purpose is to solve the problem of incompatibility between interface modifications and existing implementations.
The syntax format of the default method is as follows:
public interface Vehicle { default void print(){ System.out.println("I am a vehicle!"); } }
Consider a situation where an interface has a default method, and 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 vehicle!"); } } 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 the interface:
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 the specified interface:
public class Car implements Vehicle, FourWheeler { public void print(){ Vehicle.super.print(); } }
Java 8 Another feature of interfaces is that they can declare (and provide) static methods. For example:
public interface Vehicle { default void print(){ System.out.println("I am a vehicle!"); } // Static Method static void blowHorn(){ System.out.println("Horn sound!!!"); } }
We can understand the usage of default methods through the following code, which can be placed in Java8In the Tester.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 vehicle!"); } static void blowHorn(){ System.out.println("Horn sound!!!"); } } 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 will be:
$ javac Java8Tester.java $ java Java8Tester I am a vehicle! I am a four-wheeled vehicle! Horn sound!!! I am a car!