There are a lot of problems, such as what interfaces are used for, how to use interfaces in java, etc. In this article we hope to be able to answer some of them as easily as possible. Relatively abstract =)) and also say that the following is within the confines of our limited knowledge, if you can add something, it is really great for you and readers as well.
In the program, the book (notebook) that is your interface is. Now let's get started. You have Maths, Physics, Chemistry, Biology, .. as you mentioned, each subject is a class. You have an array of books and you need to bring them to school and let's see how we do it.
File name: School.java
You can give it a shot, then you will find that to create the array we have no choice but to set it as an Object array, and when calling the method takeBookToSchool() will not work.Now the interface will help us solve this problem. We first create a book interface that has a method called takeBookToSchool();
File name: book.java
Then we create classes Math, Physic, Chemistry, Biology implements book and implement the method takeBookToSchool() and the problem is solved as follows.
The output:
Assume that you have two classes, Interface and Download. class Interface has a button, when click on the button, call the download method in the Download class to download the file, and during the download process, the download class must respond to the Interface class to know what percentage of download is. Take a look at the following two ways.
File name: Download.java
As you have seen, updating the interface is simple as we only need to pass the interface object to the Download class. However, many problems occur when the object is passed to the download class, which is allowed to call some methods of the Interface class that it does not need. The interface update method is required to allow calling in the Download class, but here, i writes public, but not be private. Or, assume as a class, we have to go through a certain X class which is an intermediate class, then the method Download of the Download class can be called, and so, we have to pass Interface to X, then X passes to Download, which is very complicated. ... and many other restrictions that when you do the actual will understand better.
File name: Update.java
File name: Download.java
File name: Interface.java
You can see that this second can overcome the disadvantages of the first one. Class download does not need to pass object Interface, it is only needs an Update interface, so the methods, the attributes of Interface will not be "exposed" to the users or can be called from Download. If you have to pass through an X class which is an intermediate class to implement download, you do not need to pass the interface object through the intermediate classes and still update the interface during the download process. Or, assuming there are multiple interfaces that need to be updated during the download process, you can do it easily. But, with the first method, things will be much more difficult since you need to pass all those interfaces to the Download class.
Here are two applications that use the Interface that you see most clearly. I hope it can help you understand how to use the interface.
To find out more about how to use this keyword in java with detail example:
1. Use Interface in java to create common actions for objects
for example: Imagine that every day you study a lot of subjects, such as Math, Physics, Chemistry, Biology, English, Literature, History, etc. Each subject is a book or notebook, so it is inconvenient when you "compose" to class. Now I give you a way without knowing what you are learning today and still have the book of that subject when you go to class, which is using a versatile book that can hold all your subjects. I am also doing it by way of every subject in a versatile notebook, go to school only take it along=)).In the program, the book (notebook) that is your interface is. Now let's get started. You have Maths, Physics, Chemistry, Biology, .. as you mentioned, each subject is a class. You have an array of books and you need to bring them to school and let's see how we do it.
File name: School.java
File name: book.java
Then we create classes Math, Physic, Chemistry, Biology implements book and implement the method takeBookToSchool() and the problem is solved as follows.
The output:
2. Transmitting data, messages between classes
It is often said that the classes exchange data, messages through the interface. In this section we will learn about it. This issue will be clearer as we do on the interface, it means using the JFrame. But if you have not done with the interface, it is fine.Assume that you have two classes, Interface and Download. class Interface has a button, when click on the button, call the download method in the Download class to download the file, and during the download process, the download class must respond to the Interface class to know what percentage of download is. Take a look at the following two ways.
Option 1 (old way): Pass the object Layout for the Download class to update the downloaded percentage.
File name: Interface.javaFile name: Download.java
As you have seen, updating the interface is simple as we only need to pass the interface object to the Download class. However, many problems occur when the object is passed to the download class, which is allowed to call some methods of the Interface class that it does not need. The interface update method is required to allow calling in the Download class, but here, i writes public, but not be private. Or, assume as a class, we have to go through a certain X class which is an intermediate class, then the method Download of the Download class can be called, and so, we have to pass Interface to X, then X passes to Download, which is very complicated. ... and many other restrictions that when you do the actual will understand better.
Method 2: Use Interface in java
We created an interface named Update that contained the updateInterface() method; then for the interface implements Update as follows:File name: Update.java
File name: Interface.java
You can see that this second can overcome the disadvantages of the first one. Class download does not need to pass object Interface, it is only needs an Update interface, so the methods, the attributes of Interface will not be "exposed" to the users or can be called from Download. If you have to pass through an X class which is an intermediate class to implement download, you do not need to pass the interface object through the intermediate classes and still update the interface during the download process. Or, assuming there are multiple interfaces that need to be updated during the download process, you can do it easily. But, with the first method, things will be much more difficult since you need to pass all those interfaces to the Download class.
Here are two applications that use the Interface that you see most clearly. I hope it can help you understand how to use the interface.
To find out more about how to use this keyword in java with detail example:
Comments
Post a Comment