JAVA_SE basics -- 47. Interface
If all methods in an abstract class are abstract, you can define this class using another method, that is, the interface ~
When defining an interface, you must use the interface keyword to declare it. For example, code1
Interface Definition Format:
Interface name {
}
Code1
Interface Animal {int num = 1; // defines the global Variable void breathe (); // defines the abstract method void run ();}In the code above, Animal is an interface. In code1, the abstract method breathe () is not modified using the abstract keyword, because the methods and variables defined by the interface contain some default modifiers.
By default, the methods defined in the interface are modified using "public abstract.
The variables defined in the interface use "public static final" by default to modify them as global constants.
Format of the implemented interface:
Class name implements interface name {
}
Code2 (implementing the Interface)
Package day09; interface A {// member variable public static final int num = 1; // member function public void print ();} public class Demo7 {// method in the implementation interface public void print () {System. out. println (this is the print method in the interface ...);} public static void main (String [] args) {Demo7 d = new Demo7 (); d. print ();}}Code2 running result:
Interface features:
1. The class implementation interface can be implemented through implements. When implementing the interface, all methods in the interface must be implemented. A class can implement multiple interfaces.
2. All attributes defined in the interface are public static final by default. That is, since static constants are constants, they must be assigned values.
3. the method defined in the interface cannot have a method body. By default, public abstract is added to the methods defined in the interface.
4. abstract classes or interface classes are not necessarily used for abstract functions.
5. Methods in the interface are abstract by default, so they cannot be instantiated.
6. For interfaces, subclass can be used to implement functions that are not implemented in interfaces.
7. If the implementation class needs to access the members of the interface, the super keyword cannot be used. Because there is no explicit inheritance relationship between the two, the member attributes in the interface are static. You can use the interface name for direct access.
8. No constructor is available for the interface.
9. When a class implements an interface, if this class is an abstract class, partial methods in the interface can be implemented. Otherwise, all methods in the interface must be implemented.
10. When a class uses the implements keyword to implement an interface, multiple interfaces can be implemented. The Implemented interfaces must be separated by commas. For example, code3
11. An interface can inherit multiple interfaces by using the extends keyword. interfaces are separated by commas. For example, code4
12. A class can also implement interfaces while inheriting another class. In this case, the extends keyword must be before the implements keyword. For example, code5
Interface: the usb interface is mainly used to expand the functions of the notebook. The interface in java is mainly used to expand the functions of the definition class, which can make up for the disadvantages of single inheritance in java.
Code3 (one class implements multiple interfaces)
interface Run{//code……}interface Fly{//code……}class Bird implements Run,Fly{//code……}
Code4 (the interface inherits multiple interfaces)
interface A{//code……}interface B{//code……}interface C extends A,B{//code……}
Code5 (the extend keyword must be before the implements keyword)
Class A extends B implements C {// first inherited and then implemented // code ......}
Interface function:
1. Program decoupling. (Low coupling)
2. Define constraints.
3. Expand functions.
APIs are not only used for many purposes, but are also necessary. This is why we always mention the concept of "interface-oriented programming.
It doesn't matter if you don't fully understand it, but you can use it first. The benefits of the interface will be known to a certain extent after accumulation.
Why use an interface: this interface is used to handle changing situations.
The biggest advantage of an interface in the actual development process is that you can write the interface according to the design, and then assign it to everyone to work, tell a to use a written interface to implement their specific functions, and tell B to write interfaces that have already been written but do not have specific code, which can improve work efficiency. In addition, the underlying layer and the application layer are clearly layered through interfaces.
Interface Definition Format:
Interface name {
}