Interface learning notes,
Interface: only abstract classes of abstract methods can be replaced by interfaces.
Why?
The reason is as follows:
1/Multi-Inheritance
2. Design and Implementation Separation
3/more natural usage Polymorphism
4. Easier framework construction
5. It is easier to implement operations such as change and modification...
Interface is a convention
For Interface Programming: in programming, the ultimate embodiment of the spring framework is not considered as an interface-specific agreement.
Focus on the capabilities of the class rather than the Implementation Details
Interfaces cannot be instantiated, but interfaces can be implemented.
========================================================== ==============
All the attributes of the interface are modified by public static final.
Constants can be basic data classes or reference data types.
Note:
Constant naming rules all uppercase letters
The interface modifier may be at the packet access level, so public constants may not be accessible.
We recommend that you use enumerative to save a set of first-off data values above JDK 5.
Interface member ---- Method
1. abstract Method
2/static method
3. The last two default methods are added by Java 8.
An interface can inherit from an interface and cannot inherit static methods.
========================================================== ======================
A class can implement multiple interface interfaces and can be used as data types to declare variables.
Abstract classes that implement interfaces can override abstract methods in some interfaces.
----------------------------------------------------------------------------
New syntax proposed by Java 8
Static Method of interface 1
1. You can declare static methods in the interface to implement some processing logic.
2. This method will not be inherited by implementation classes and subinterfaces.
In this way, we can directly call the static method in the interface when using the interface, but we can only call the method in this way.
Default method of the Second Interface
The default method provides default implementation for all classes that implement the modified interface.
There can be multiple default methods
It cannot be static.
You can override the default method.
Why is there a default method? Previously, the method was declared through the interface to implement the method in the implementation of the interface. However, this may happen in a running program.
A new method may be required for an interface, and the Implementation classes of this interface must implement this method and make corresponding modifications.
Based on this consideration, the java8 designer proposed such a method ....
For example, the Implementation class of the interface ---> default viod show () {} has such an action.
The default method is compared with the common method of the class:
Similarities: All provided implementations can use this to represent the currently running instance
Difference: the common method of the class can access the instance to the variable, but the default method is not. It can only access other members of the interface.
---> The default method proposed by different points can only access other members of the interface, but cannot access instance variables because it is logically unavailable.
Very simple, except for existing interfaces and subsequent implementation classes... Override Methods
========================================================== ==================================
Here we will make three parts and record 1/resolve the conflict 2/mark the Interface 3/functional interface
1. What is a conflict ??? In Java 8, the default method has advantages and disadvantages. One of its drawbacks is conflict.
For example, Class A has A method show () B interface, which has A default method show (). An implementation class inherits the Class A and implements the B interface at the same time. At this time, the instance of this implementation class
When the show method is called, a conflict occurs. The default method of the interface conflicts with the parent method.
---> Conclusion: when the parent class conflicts with the interface method, the parent class takes precedence.
The default method cannot be the same as the method in the Object. Why? Because the Object is the parent class of all classes, there is no possibility of calling in the interface.
If the default method names of multiple interfaces are the same and the parameters are different, the default method in the interface can be reloaded according to the rewrite rules.
When multiple interfaces have identical default methods, subclass must override this method.
-------------------------------------------------------------------------------
Mark interface? No member interface, no member, and no member inherited from the parent interface
What's the purpose? Used to mark a class that has specific meaning in a specific environment
In jdk
1./java. lang. Cloneable indicates that this class can be cloned.
2./java. io. Serializable indicates that this class can be serialized.
3./java. rmi. The Remote flag interface is related to the Remote call method ...?
Java 5 introduces the annotation syntax to implement the same marking Function and the performance is relatively better.
========================================================== ======================
Function interface: Only one abstract method interface, so you can skip the anonymous internal class () -->
@ FunctionalInterface: Mark the function interface annotation jdk8. This annotation is optional.
Objective: to associate with a lambda expression
View comments