I read the basics of Java. I have read different books and have some knowledge about Java.
I feel confused about interfaces.
Interfaces also know the specifications for using interfaces written in books, as well as interfaces used for multithreading, interface serialization, and interface listening.
But still confused, write your own program, such as some practical programs, do not know when the interface must be used ~!
I am confused about how to use the interface myself. It is estimated that it is related to beginners who are not too skilled or too knowledgeable. I only know how to use interfaces for listening program code in books.
Therefore, I would like to ask you to give an example of the interface to explain why the interface must be used. Or give me some in-depth and detailed articles on the interface.
Thank you.
Reply
The interface only defines what should be done, but does not define how to do it.
Therefore, if your program is completed by only you, and each function has only one implementation method, I think you can, note that yes, no interface
If many people work together, you may develop quickly. Unfortunately, a class you need is not completed yet. What do you do? Wait for him?
If it is interface-oriented, You can first write the simplest implementation to debug your code. Spring is a good helper.
If a function has multiple implementations, such as exporting documents. Some require HTML, some include Word, Excel, PPT, and PDF.
Because you cannot predict what they actually need. At this time, you can use interfaces to require each implementation to complete the export method.
Then, you made an Excel file and completed other items as needed. At this time, your original program does not need to be modified because they call interfaces, instead of a specific implementation class.