We used to say that Java is an "object-oriented Programming", and now we're talking more about "object-oriented programming." The visibility of the interface in the Java language is high.
First, what is an interface? In the software interface is a specification and standards, they can constrain the behavior of the class, is a collection of methods, but there is no method of implementation, the interface can actually be regarded as a special abstract class, but the use and abstract class completely different methods to express, the design of the two concepts are different, abstract class is conducive to code reuse, The interface facilitates code expansion and maintenance.
Second, the role of the interface: In the Java language, abstract class and interface are the two mechanisms that support the definition of abstraction classes. It is the existence of these two mechanisms that gives Java a powerful object-oriented capability. Interfaces allow classes that do not have an inheritance relationship (is a relationship) to have inherited behavior. At the same time, the interface makes use of the multi-implementation mechanism to solve the problem of insufficient richness of single inheritance in Java, making the Java language richer and simpler!
Third, the syntax of the interface:
1. Interface definition Syntax: Change the class keyword of the definition classes to the interface keyword, and the access modifier can only be public or the default same package. Property---can only be public, static, and constant properties---even if these modifiers are not written, they will default to public static constants. Design interfaces are more concerned with the design of behaviors (allowing classes with no inheritance to share rows
Instead of the focus property.
2. All the methods in the interface are abstract methods, and the methods in the interface are automatically modified with public, that is, there is only a global abstract method in the interface.
3. Interfaces cannot be instantiated and cannot be constructed in interfaces.
4. An inheritance relationship can be implemented between interfaces through extends, an interface can inherit multiple interfaces, but the interface cannot inherit classes.
5. The implementation class of the interface must implement all the methods of the interface, only the public abstract method----even if it is not written, it will default to the public abstract method.
6. The meaning of the design interface is to share the behavior with different classes (no inheritance relationships), but each class has its own implementation.
Three, the difference between the interface and the abstract class:
1. Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;
2. member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;
3. The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
4. A class can inherit only one abstract class, while a class may implement multiple interfaces.
class implements an interface, but does not have an interface to implement the class (as long as a method is not implemented, the class is defined as an abstract class). The reference to the interface can point to the object of the class. But only access to subclass objects overrides the definition of the method in the interface! Interface inheritance interfaces can be multiple-inheritance interfaces!
Understanding of interfaces in Java