Java interface new features (JAVA8)

Source: Internet
Author: User

JAVA8 new features interface enhancements

In Java7 and previous versions, the methods in the interface are abstract and there is no static method, and the property default modifier is public static final. All method declarations are public [return type] [method name] (argument list) [throws exception list].

There are 2 concrete implementations of the interfaces defined in Java8, as follows:

1 static

there can be static methods: public static [return type] [method name] (argument list) [throws exception list]

Multiple interfaces can be implemented in Java, and if 2 interfaces have the same method name and static methods of the same parameter list (that is, the method signature is the same), then the [subclass. Parent static method] does not know which one to call. Therefore, the use of the [subclass. Parent static method] is not allowed, and the static method can only be called by the parent class name, resolving the conflict.

public class Cimpl implements Intera, Interb

where Intera and Interb have static methods with the same name, subclasses can also have methods that have the same method signature, but cannot be overridden with override, because static methods cannot be rewritten.

The method of the static type is called by the interface name.

2 Default

Method can have a default implementation:

Public default void Defaultmethod () {} (default cannot be omitted and can only be modified in the interface)

When a subclass overrides the default method for an interface, you must remove default.

@Override  Public void Defaultmethod () {    System.out.println ("default modifier in subclass a must be removed");}

Defines a class that implements the interface through which an instance of the class invokes the method of the default type.

Advantages

1 to avoid duplication of subclass code

When multiple classes implement a method of an interface, if the method's implementation code is the same, it can cause code duplication problems. The interface enhancement is equivalent to extracting the common code into the interface definition, the implementation class does not need to override the method, and solves the problem of repeating the subclass code that implements the interface.

2 Overcoming the defects of single inheritance

Interfaces can define either static or default types, which are implemented in a way that reduces the distinction between interfaces and abstract classes. For subclasses, abstract classes can only be single-inherited, and interfaces can implement more than one.

Resources

jdk1.8 new Features-interface

Interface summary of new features of JDK8

Java8 Interface Enhancements

Java interface new features (JAVA8)

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.