Interface and entity class
This is the back-end small class of the monastery. Each article is shared from
[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]
Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:
[interface and entity class]
Interface and entity class
-Shenzhen Java Janus-
1. Background introduction
2. Knowledge analysis
3. Frequently asked questions
4. Solutions
5. Coding practice
6. Expand thinking
7. References
8. More discussion
1. Background introduction
Why use an interface instead of directly using an entity class to complete the task? What are the benefits of interface and impl?
Both interface and class are java files. Interface implementation classes and directly written entity classes can actually complete the functions we want. Why not write them directly as implementation classes? Why prefer interfaces?
Java interface is a declaration of a series of methods and a collection of method features. An interface has only method features and no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors
2. Knowledge analysis
What is an entity class?
Entity classes are common and easy to understand. They are just ordinary Java classes:
Member variable
There are constructors, methods, or main functions
You can directly new an object to use
You can extend
What is an interface?
Interface is a class that needs to be implemented. It is a very important concept in Java
Construction methods and other methods generally do not have their own implementation (default can be implemented, which will not be considered for the time being)
Generally, they do not have their own member variables. If they do, they are public, static and final by default, and must be initialized, that is, the member variables in the interface are constants
The java interface must implement its abstract methods through classes and implements. There can be more than one implementation class
At the same time, a class can also implement multiple interfaces and multiple inheritance at the same time
There is no constructor in the interface and cannot be instantiated. In addition to the construction method, there are other methods, but only the type of return value, the number and type of parameters are determined. By default, they are public and abstract types. There is no method body and cannot be instantiated
When a class implements a java interface, it must implement all abstract methods in the interface, otherwise the class must be declared as an abstract class
You can extend
3. Frequently asked questions
Why prefer to use interface?
Students' doubts: I have defined an interface, but I have to write the implementation method of the interface in the class that inherits the interface. Wouldn't it be more convenient for me to write the implementation method directly in this class without defining the interface
4. Solutions
Abstract real problems: an idea of OOP is that it does not need to know the specific implementation of the opposite client, but can be used directly by understanding the number of input parameters, types, return value types and exceptions through documents. As for the specific implementation, it can be ignored. Implementation has developers to care about.
Specification: if you want to change the specific content of the implementation in the future, just change the implementation class, and the interface does not need to be moved. This greatly reduces the coupling.
Polymorphism: an interface can have multiple implementations, and a class can implement multiple interfaces.
5. Coding practice
6. Expand thinking
Abstract classes and entity classes
Abstract classes and interfaces
Abstract class is the abstraction of the properties of real problem objects
The abstraction of object methods when there are more interfaces
7. References
Information provided by CSDN, Baidu Encyclopedia and senior brothers
8. More discussion
When to use the interface? When to use entity classes?
What should I pay attention to when writing interfaces?
What should I pay attention to when writing implementation classes?
Thank you for reading. If you have any questions, please criticize and correct them
By Shenzhen Branch - Janus
Ppt link video link