Improved Java 8 interfaces and improved Java 8 Interfaces

Source: Internet
Author: User

Improved Java 8 interfaces and improved Java 8 Interfaces

Java 8 has improved the interface,You can define the default method in the interface. The default method can provide the method implementation.

5.6.1 interface concepts

A class is a specific implementation class, and an interface defines a specification. An interface defines the specifications that a batch of classes must comply with. The interface does not care about the internal status data of these classes, it does not care about the Implementation Details of methods in these classes. It only specifies that some methods must be provided in these classes, and classes that provide these methods can meet actual needs.
An interface is a specification abstracted from multiple classes. An interface does not provide any implementation. Interfaces reflect the design philosophy of standardization and implementation separation.

5.6.2 definition of interfaces in java

Example:[Modifier] interface Name extends parent interface 1, parent interface 2... {}

The interface name can usually use adjectives
Whether or not the public static final modifier is used, the member variables in the interface always use these three modifiers.

The internal class, internal interface, and internal enumeration defined in the interface all use the public static Modifier by default. The system automatically uses the public static modifier to modify them.
Output. java

Package code; public interface Output {// The member variables defined in the interface can only be constant int MAX_CACHE_LINE = 50; // The normal method defined in the interface can only be the public abstract method void out (); void getDate (String msg); // the default method defined in the interface, use default to modify default void print (String... msgs) {for (String msg: msgs) {System. out. println (msg) ;}// the default method defined in the interface. You need to use default to modify default void test () {System. out. println ("default test () method");} // defines class methods in the interface. You must use static to modify static String staticTest () {return "class methods in the interface ";}}

Default is different from the default of the Access Controller.

Java 8 allows you to define the default method in the interface. The default method must be modified by default. This method cannot be modified by static. The default method is automatically used.Public modifier (required). Because the default method does not have static modification, you cannot directly use the interface to call the default method.Interface implementation class instanceTo call these default methods.
The class method defined in the interface must be modified using static, cannot be modified using default, and automatically added to the classPublic (required). Class methods can be called Directly Using Interfaces

package code;public class OutputFieldTest{    public static void main(String[]args){        System.out.println(code.Output.MAX_CACHE_LINE);        //code.Output.MAX_CACHE_LINE = 20;        System.out.println(code.Output.staticTest());    }}

The default member variables in the interface areUse public static finalIn different packages, you can also access the member variables in the interface through interfaces.

5.6.3 interface inheritance

Interfaces support multi-inheritance,

package code;interface interfaceA{    int PROP_A = 5;    void testA();}interface interfaceB{    int PROP_B = 6;    void testB();}interface interfaceC extends interfaceA,interfaceB{    int PROP_C = 7;    void testC();}public class InterfaceExtendsTest{    public static void main(String []args){        System.out.println(interfaceC.PROP_A);        System.out.println(interfaceC.PROP_B);        System.out.println(interfaceC.PROP_C);    }}

5
6
7

5.6.4 Interface

Conclusion: The interface has the following purposes:

  • Defines variables and can also be used for forced type conversion.
  • Constant defined in the call interface
  • Implemented by other classes

Example:[Modifier] class name extends parent class implements interface 1, interface 2... {Class part}

Implements must be placed after the extends part
After a class implements one or more interfaces, this class must fully implement all the abstract methods defined in these interfaces (that is, rewrite these abstract methods ), otherwise, the abstract methods inherited from the parent interface will be retained. This class must also be defined as an abstract class.

Printer. java

Package code; interface Product {int getProduceTime ();} public class Printer implements Output, Product {private String [] printData = new String [MAX_CACHE_LINE]; private int dataNum = 0; public void out () {while (dataNum> 0) {System. out. println ("printer printing:" + printData [0]); System. arraycopy (printData, 1, printData, 0, 1); -- dataNum ;}} public void getData (String msg) {if (dataNum >=max_cache_line) {System. out. println ("the output queue is full, adding failed");} else {printData [dataNum ++] = msg ;}} public int getProduceTime () {return 45 ;} public static void main (String [] args) {Output o = new Printer (); o. getData ("lightweight JavaEE enterprise application practices"); o. getData ("Crazy Java handout"); o. out (); o. getData ("crazy Android handout"); o. getData ("Crazy Ajax handout"); o. out (); o. print ("Sun Wukong", "", "Bai Gujing"); o. test (); Product p = new Printer (); System. out. println (p. getProduceTime (); Object obj = p ;}}

Printer printing: lightweight JavaEE Enterprise Application Practice
Printer printing: Crazy Java Handouts
Printer print: crazy Android handout
Printer print: Crazy Ajax Handouts
Sun Wukong
Pig
Bai Gujing
Default test () method
45

The above program shows that the Printer class implements the Output interface and the Product interface, so the Printer object can be directly assigned to the Output variable or the Product variable, as if the Printer class is both a subclass of the Output class and a subclass of the Product class, this is what java providesSimulate multi-Inheritance

Printer implements the Output interface to obtain the print () and test () default methods defined in the Output interface. Therefore, the Printer instance can directly call these two default methods.

The interface cannot inherit classes explicitly, but all referenced variables of the interface type can be directly assigned to reference variables of the Object type.

5.6.5 interfaces and abstract classes

Common features:

  • Cannot be instantiated, all at the top of the inheritance tree for implementation and inheritance
  • Can contain abstract methods. Implementation classes and interfaces must implement these abstract methods.
    Differences:

Interfaces are similar to the general principles of the entire system, and the standards that should be followed by various modules of the system are formulated,Therefore, the interface in a system should not be changed frequently.Once the interface is changed, the impact on the entire system and even other systems is radiation-like, resulting in the re-rewriting of most classes.

An abstract class is a modular design. As an abstract parent class of multiple sub-classes, it can be used as an intermediate product in the system implementation process. This intermediate product has implemented some of the system's functions (those that have provided implementation methods), but needs to be improved.

Differentiation Interface Abstract class
1 It can only contain abstract methods and default methods, and cannot provide methods for common methods. It can completely include common methods
2 Static methods cannot be defined You can define static methods.
3 Only static variables can be defined, and common member variables cannot be defined. Both common member variables and static variables can be defined.
4 No Constructor The constructor can contain constructor. The constructor of an abstract class is not used to create an object. Instead, it allows its subclass to call these constructor to complete initialization of an abstract class.
5 The initialization block cannot be included. It can completely include the initialization block.
6 One class can directly implement multiple interfaces. By implementing multiple interfaces, you can make up for the shortcomings of Java single inheritance. A class can have at most one direct parent class, including abstract classes.

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.