Understand Java interfaces and Java Interfaces
I. Why should I use interfaces?
If you have a requirement: You must implement the Anti-leech function. The door has the "open" and "off" functions, and the lock has the "Lock" and "unlock" functions.
Analysis: first, the anti-theft door is a door, which has the function of opening and closing the door, as well as a lock, which has the functions of unlocking and locking. According to the idea of object-oriented programming, we will separate the door and lock as a class. However, we cannot let the anti-leech door inherit from the door and inherit from the self-locking. The anti-leech door is not a lock and does not conform to the relation of is a in the inheritance, java supports single inheritance. Then we need to use interfaces to solve this problem.
2. What is an interface?
In software, interfaces are a kind of specification and standard. They can constrain the behavior of classes and are a set of method features, but there is no way to implement them, the interface can also be seen as a special abstract class, but it is expressed in a completely different way from the abstract class. The design concepts of the two are also different. abstract classes are conducive to code reuse, interfaces facilitate code extension and maintenance.
Iii. Differences between abstract classes and interfaces:
01 abstract classes can provide implementation details of member methods, while interfaces can only have public abstract methods;
02. The member variables in the abstract class can be of various types, while the member variables in the interface can only be of the public static final type;
03. The interface cannot contain static code blocks and static methods. abstract classes can have static code blocks and static methods;
04. A class can inherit only one abstract class, but a class can implement multiple interfaces.
4. How to define interfaces
First, let's look at the Syntax:
[Modifier] interface Name extends parent interface 1, parent interface 2 ,...
{
// Constant definition
// Method definition
}
Implement the interface syntax in a class:
Class name parent class name implements interface 1, interface 2 ,.....
{
// Class member
}
Iv. Notes for defining Interfaces
01. The interface naming rules are the same as those of the class. If the modifier is public, the interface is visible throughout the project. If the modifier is omitted, the interface is only visible in the current package.
02. A constant can be defined in an interface, but a variable cannot be defined. If you define an attribute in an interface, you can see that it is automatically modified using public static final through decompilation. All attributes in an interface are global static constants, the constant in the interface must be specified at definition.
03. All methods in the interface are abstract methods. All methods in the interface are automatically modified using public abstract, that is, only global abstract methods are available in the interface.
04. The interface cannot be instantiated and cannot be constructed in the interface.
05 interfaces can be inherited through extends. One interface can inherit multiple interfaces, but the interface cannot inherit classes.
06. The interface implementation class must implement all the methods of the interface, otherwise it must be defined as an abstract class.