Default methods and Static methods in Java 8--interface

Source: Internet
Author: User
Tags java se

Before Java SE 8, interface was just an abstraction of things, used to define unified abstractions and abstract behaviors and attributes that describe things.

However, in Java SE 8, the static behavior of behaviors and things that can be added to the default implementation in interface is added.

A. Why?

Java iterates over so many versions and primarily uses evolutionary ideas rather than refactoring (to ensure compatibility with history).
This makes it easy to get a lot more difficult to deal with, and the interface is trained after it is released, unless we can update all implementations of that interface at once. For example, a history interface has many implementations, need to add a behavior in the history interface, so long need to implement the behavior in all its implementation, then undoubtedly hinder the evolution, adding the default method to the interface can make the interface can still evolve gradually, which is also in line with the law of natural things have been developing.

And, son. In Java SE 8, a functional interface is introduced, and the default methods and static methods in the interface are used to break through the individual method limitations of the functional interface.

Two. Example

The default method in the interface

public interface TestDefaultMethodApi {    default void print(String msg) {        System.out.println(msg);    }}

Static methods in the interface

public interface TestStaticMethodApi {    static void utilMethod(String msg) {        System.out.println(msg);    }}
Three. Use

Default method:

    1. Can be used in a function-type interface
    2. The public behavior can be abstracted as the default method, which is the default method in all implementations-this can refer to collection
    3. You can add a default method to an already published interface, allowing the interface to behave in a new way and continue to evolve

static method:

    1. Statically more property interfaces or types behave themselves, and some tool methods that are nearly interface-coupled can be used as static methods to achieve high-cohesion
Four. Points of caution used
    • interface, the default method is defined in the implementation, but the default method does not need to be implemented, and if the implementation does not overwrite the default method defined in the interface, the ability to invoke the default method in the implementation is the default method implemented in the interface

    • The implementation of the default behavior can be overridden in a specific implementation, such as vector vectors that cover the Removeif in collection

    • The default method can only be defined in the interface, and if it is defined elsewhere, the compilation error

Reference

Deep understanding of Java 8 Lambda (language article--lambda, method reference, target type and default method)

Default methods and Static methods in Java 8--interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.