Java 8 introduces several major features compared to Java 7 (features) (interface default method), methods in interface, (interface static methods) static method in interface, function programming (Functiona L programming), LAMDA expression, stream API.
Here, the following default interface methods are introduced first
1. What is the default interface method
Java 8 allows the implementation method to be defined and written on the interface. Example:
Interface collection{void Add (); void remove (); New feature in Java 8//default methoddefault Void put () {System.out.println ("Put the key in");}}
As can be seen from the example, Java 8 adds the new keyword default to implement a method in the interface definition and implementation.
2. Why a default interface method is required
The syntax for the default interface method is simple, but why Java 8 needs to introduce this new concept is great article.
Here I give a realistic example, now we need to add a new method to an interface called collection, and all classes that implement this interface can call this method. This can be achieved in Java 7, but you need to rewrite the override method for each class that implements this interface. This is really bad, because you need to modify each method that implements the interface class. Otherwise there is no way to compile.
However, if the interface implementation method (the default interface method) is allowed, this problem can be solved well. Not much nonsense to say first on the example:
interface collection{void add (); Void remove ();//new Requirement: project need add a new method//but we can ' t Overrided the method in each implements classes//default method is to solve this problemsdefault void put () {System.out.println ("put the Key in ");}} class list implements collection{@Overridepublic void add () {// TODO Auto-generated method stub} @Overridepublic void remove () {// TODO Auto-generated method stub}}public class whyneeddefault {public static void main (String[] args) {list list = new list (); List.add ();//now list have new method putlist.put ();}}
The above example simply takes advantage of the interface default method to add a new put method for all classes that implement the collection interface. And the list class does not require any code modifications. The last List.put () can be called directly.
3 Some details to be aware of using the default interface method
3.1 When a class implements multiple interfaces and multiple interfaces have the same method
Suppose: Idefaultmethods, the Iregular interface has a method called void talk ()
Java8 compiler will error if FIANLCLASS2 does not rewrite the override Talk () method
Error would show if IDM and IRr have the same method define unless overrided the talk Methodpublic class FinalClass2 imple ments idefaultmethods, iregular{public static void Main (string[] args) {} @Overridepublic void Move () {//TODO Auto-generat Ed method stub} @Overridepublic void Fly () {//Todo auto-generated method stub} @Overridepublic void Talk () {//Todo Auto-ge Nerated method StubSystem.out.println ("Final Class 2 Talk method");
3.2 When a class inherits a class and implements multiple interfaces. This parent class and multiple interfaces have the same method
Suppose: ClassA, Idefaultmethods, iregular interfaces have a method called void talk ()
Compile results this time may be another disappointment, the compiler did not error even if finalclass4 do not rewrite the talk () method.
Because ClassA will be prioritized call talk () > interface method Talk ()
No error shows even FC4 not overrided the talk method//error only happen if compiler can ' t figure out which method need To IS Calledpublic class FinalClass4 extends ClassA implements idefaultmethods,iregular{@Overridepublic void Move () {//To Do auto-generated method stub} @Overridepublic void Fly () {//TODO auto-generated method stub}public static void main (Strin G[] args) {FinalClass4 fc4 = new FinalClass4 (); Fc4.talk (); Output ClassA's Talk method}
This article is from the "Development" blog, make sure to keep this source http://jamesdev.blog.51cto.com/2066624/1857186
Java 8-interface Default Method interface