The essence of interface - An interface is a special abstract class. This abstract class only contains the definition of constants and methods, without the implementation of variables and methods.
Interfaces can have some things that abstract classes have. If all the methods in an abstract class are abstract, no method needs to be implemented by this abstract class, and all the variables in this abstract class are static variables. , are all variables that cannot be changed (final). At this time, such an abstract class can be defined as an interface. The format for defining a class as an interface is to replace the keyword class that declares the class with the keyword interface that declares the interface.
An interface is a special abstract class. In this abstract class, all methods are abstract methods, and the attributes (i.e. member variables) of this abstract class are declared as "public static final type attribute names" In this way, the default declaration is "public static final", that is, the member variables inside are public, static, and cannot be changed. Therefore, when declaring a constant in an interface, you can write it in the form of "public static final type constant name=value(value)", or you can directly write it in the form of "type constant name=value(value)" such as: "public static final int id =10" can be directly written in the form of "int id=10", because the default property declarations in the interface are all "public static final", so "public static final" can be omitted. The abstract method declared in the interface does not need to be identified by the abstract keyword, because all methods in the interface are abstract, so the "abstract" keyword is omitted by default. For example, if you declare such three words in an interface, Methods: "public void start()", "public void run()", "public void "stop()" are not identified by the abstract keyword in front of these three methods, but they are abstract methods, because the methods declared in the interface are all abstract methods, so the abstract keyword will be omitted in the abstract methods in the interface. Dropped, because the methods declared by default are abstract , so there is no need to write the word "abstract". This is different from when declaring abstract methods in abstract classes. When declaring abstract methods in abstract classes, you must use the "abstract" keyword, and in interfaces You can omit "abstract" when declaring abstract methods. Note: The abstract method declared in the interface is "public" by default, and can only be "public". The reason why it is declared in this way is to correct problems that easily occur when multiple inheritance occurs in C++. , multiple inheritance in C++ is prone to problems. The problem is that if multiple parent classes of multiple inheritance have the same member variables, it will be quite troublesome to reference them, and various problems will occur during operation. . In order to correct this problem, JAVA changed all the member variables in the interface to static final. If the member variable is of static type, then the member variable belongs to the entire class, rather than being exclusive to a certain object. For multiple inheritance, a subclass object actually contains multiple parent class objects, while for single inheritance, there is only one parent class object in the subclass object. Multi-inherited subclass objects have multiple parent class objects, and there may be duplicate member variables between these parent class objects, which is very prone to problems. Therefore, this problem can be avoided in JAVA by using Interfaces are used to implement multiple inheritance. As an interface, a class can inherit from the interface (or implement the interface). This is also multiple inheritance. The member variables in the interface are not exclusive to a certain object. They are all static member variables and belong to the entire class. Therefore, a It doesn't matter if a class implements multiple interfaces, and there will be no conflict between objects. Implementing multiple interfaces also achieves multiple inheritance, and avoids the problems prone to multiple inheritance. This is the benefit of using interfaces to implement multiple inheritance.
1. Define the interface
Use interface to define an interface. Interface definitions are similar to similar definitions. They are also divided into interface declarations and interface bodies. The interface body consists of two parts: constant definition and method definition. The basic format for defining an interface is as follows:
[Modifier] interface interface name [extends parent interface name list]{
[public] [static] [final] constant;
[public] [abstract] method;
}
Modifier: Optional, used to specify the access permission of the interface, the optional value is public. If omitted the default access permissions are used.
Interface name: A required parameter, used to specify the name of the interface. The interface name must be a legal Java identifier. Generally, capital letters are required.
extends: Parent interface name list: optional parameter, used to specify which parent interface the interface to be defined inherits from. When using the extends keyword, the parent interface name is a required parameter.
Methods: The methods in the interface are only defined but not implemented.
For example, define an interface for calculation, in which a constant PI and two methods are defined. The specific code is as follows:
public interface CalInterface { final float PI=3.14159f;//Define the constant PI used to represent pi float getArea(float r);//Define a method for calculating area getArea() float getCircumference(float r);// Define a method getCircumference() for calculating perimeter }
Notice:
Like Java's class files, the file name of the interface file must be the same as the interface name.
2. Implement the interface
After an interface is defined, it can be implemented in a class. To implement an interface in a class, you can use the keyword implements, and its basic format is as follows:
[Modifier] class <class name> [extends parent class name] [implements interface list]{
............
}
Modifier: Optional parameter, used to specify the access permission of the class. The optional values are public, abstract and final.
Class name: A required parameter, used to specify the name of the class. The class name must be a legal Java identifier. Generally, capital letters are required.
extends parent class name: optional parameter, used to specify which parent class the class to be defined inherits from. When using the extends keyword, the parent class name is a required parameter.
implements interface list: Optional parameter used to specify which interfaces this class implements. When using the implements keyword, the interface list is a required parameter. When there are multiple interface names in the interface list, separate them with commas.
When implementing an interface in a class, the method name, return value type, number and type of parameters must be exactly the same as those in the interface, and all methods in the interface must be implemented. For example, write a class named Cire that implements the interface Calculate defined in Section 5.7.1. The specific code is as follows:
public class Cire implements CalInterface { public float getArea(float r) { float area=PI*r*r;//Calculate the circle area and assign it to the variable area return area;//Return the calculated circle area} public float getCircumference(float r) { float circumference=2*PI*r; //Calculate the circumference of the circle and assign it to the variable circumference return circumference; //Return the calculated circumference} public static void main(String[] args) { Cire c = new Cire(); float f = c.getArea(2.0f); System.out.println(Float.toString(f)); } }
In class inheritance, only single inheritance can be done, but when implementing an interface, multiple interfaces can be implemented at one time, and each interface is separated by a comma ",".
At this time, constant or method name conflicts (in multiple interfaces) may occur. When solving this problem, if the constants conflict, you need to explicitly specify the interface of the constant. This can be achieved through "interface name.constant". If a method conflict occurs, you only need to implement one method.