1. Why use interfaces
If there is a requirement: it requires the function of the anti-theft door. The door has the functions of "open" and "close", and the lock has the functions of "locking" and "opening".
Analysis: First of all, the anti-theft door is a door. The door has the functions of opening and closing the door, and there is also a lock. The lock has unlocking and locking. According to the idea of object-oriented programming, we will regard both the door and the lock as a class and exist separately. However, the anti-theft door cannot be inherited from the door and the self-locking. The anti-theft door is not a lock, which does not conform to the relationship of is a in inheritance. Single inheritance is supported in Java. So how do we solve this problem? At this time, we need to use the interface.
2. What is an interface
In software, interfaces are a specification and standard. They can constrain the behavior of classes and are a collection of some method features. However, there is no implementation of methods. Interfaces can actually be regarded as a special abstract class, but they use completely different methods from abstract classes to represent them. The design concepts of the two are also different. Abstract classes are conducive to code reuse, and interfaces are conducive to code expansion and maintenance.
3. The difference between abstract classes and interfaces:
01Abstract classes can provide implementation details of member methods, while only public abstract methods can exist in the interface;
02. Member variables in abstract classes can be of various types, while member variables in interfaces can only be of public static final type;
03. The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
04. A class can only inherit one abstract class, while a class can implement multiple interfaces.
4. How to define an interface
Let’s first look at the syntax:
[Modifier] interface interface name extends Parent interface 1, Parent interface 2,...
{
//Constant definition
//Method definition
}
Implement interface syntax in a class:
class class name parent class name implements interface 1, interface 2,...
{
//Class Member
}
5. Define the points of attention in interface
01. The naming rules of the interface 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. Constants can be defined in the interface, but variables cannot be defined. If you define attributes in the interface, then through decompilation, you can see that it will automatically modify it with public static final. The attributes in the interface are all global static constants, and the constants in the interface must specify the initial value when defining.
03. All methods in the interface are abstract methods, and methods in the interface will be automatically modified with public abstract, that is, there are only global abstract methods in the interface.
04. The interface cannot be instantiated and there cannot be a structure in the interface.
05. The inheritance relationship can be realized through extends between interfaces. One interface can inherit multiple interfaces, but the interface cannot inherit classes.
06. The interface implementation class must implement all methods of the interface, otherwise it must be defined as an abstract class.
The above article in-depth understanding of the interfaces in Java is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.