[Java] Using the interface
There are many issues raised, such as user interface to do, why use interface, How to use interface,… In this article I hope to be able to answer somewhat understandable as possible because it is usually relatively abstract =)) and also say what is written below is part of my limited understanding, if you can supplement is something really great for me as well as you read tracking.
Content
1. Use Interface joint action to create objects
For example, always offline. Imagine that every day you learn a lot of subjects, Mathematics, Ly, Chemistry, Born, English, Van, History,… each subject a book or play, so really inconvenient when you “composed of” to class. Now I present to you a way that does not need to know today what is studied with the goal of which come to class, that use of versatile that can accommodate all your subjects. Themselves are made that way by all subjects in 1 versatile notebook, school only just picked it off is finished =)).
In programming, the book (register) versatile interface that is your. Now start trial offline. You Mathematics, Ly, Chemistry, Born,.. like I just mentioned, each module is a class. You have an array of books and need to bring it to school and we try to see how we normally?
package cachhoc.net.useinterface.method; class Toan { public void mangSachToiTruong() { System.out.println("Sach Toan duoc mang toi truong"); } } class Ly { public void mangSachToiTruong() { System.out.println("Sach Ly nhe"); } } class Hoa { public void mangSachToiTruong() { System.out.println("Sach Hoa ok"); } } class Sinh { public void mangSachToiTruong() { System.out.println("Sach Sinh day roi"); } } public class TruongHoc { public static void main(String[] args) { Object[] sach = { new Toan(), new Ly(), new Hoa(), new Sinh() }; for (Object s : sach) { s.mangSachToiTruong(); // loi roi nhe } } }
You go try code, you will see is to create arrays sach we no other way to put it is the Object array and call the method mangSachToiTruong() will not be possible.
Now the interface will help us to solve this problem. First we create an interface Books there is method mangSachToiTruong();
package cachhoc.net.useinterface.method; public interface Sach { public void mangSachToiTruong(); }
Then we create the class Toan, Glass, Flower, Born and executable implements methods Books mangSachToiTruong and problems are resolved as follows.
package cachhoc.net.useinterface.method; class Toan implements Sach { @Override public void mangSachToiTruong() { System.out.println("Sach Toan duoc mang toi truong"); } } class Ly implements Sach { @Override public void mangSachToiTruong() { System.out.println("Sach Ly nhe"); } } class Hoa implements Sach { @Override public void mangSachToiTruong() { System.out.println("Sach Hoa ok"); } } class Sinh implements Sach { @Override public void mangSachToiTruong() { System.out.println("Sach Sinh day roi"); } } public class TruongHoc { public static void main(String[] args) { Sach[] sach = { new Toan(), new Ly(), new Hoa(), new Sinh() }; for (Sach s : sach) { s.mangSachToiTruong(); } } }
2. Data transmission, messages between the class
It is often said that the class data exchange, message through the interface. In this section we will learn it. This issue will be clearer if we do on the interface, ie using the JFrame. But if you do not interface well with no stars.
You have 2 class is GiaoDien and Download. GiaoDien class has a button, Clicking on the download method call in class Download to download the file, and during download, class download to get back to class GiaoDien know how much is download and. You see 2 the following methods of yourselves.
How to 1 (How old corn or used batons): Communication objects for the class interface to update Download percent downloaded.
package cachhoc.net.useinterface.senddata; public class GiaoDien { private Download download; /** * phuong thuc khoi tao Giao dien, dong thoi khoi tao doi tuong download va * truyen chinh giao dien sang class Download de cap nhat giao dien */ public GiaoDien() { download = new Download(this); } private void nhanDownload() { download.download(); } public void capNhatGiaoDien(int phanTramDownload) { System.out.println("class giao dien cap nhat duoc: " + phanTramDownload + "%"); } public static void main(String[] args) { GiaoDien giaoDien = new GiaoDien(); giaoDien.nhanDownload(); } }
package cachhoc.net.useinterface.senddata; public class Download { private GiaoDien giaoDien; /** * @param giaoDien * de cap nhat giao dien */ public Download(GiaoDien giaoDien) { this.giaoDien = giaoDien; } public void download() { for (int i = 0; i < 100; i++) { System.out.println("Dang download..."); giaoDien.capNhatGiaoDien(i); } System.out.println("Ket thuc download"); } }
As you have seen, the updated interface as simple proved that we just passed the interface to the class object is finished Download.
However, many problems occur when the object is passed to giaoDien, download class is allowed to call a method of the class of which it is not necessary GiaoDien, updated interface methods required to allow calls in class Download it can be called that here alone to the public and not the private. Or suppose that the class we have to go through a certain class X in the middle of class, the method download Download new call and so we have to transfer to the X GiaoDien, X then transmitted to Download, Such complex… and many other restrictions that will actually make you a better understanding.
How to 2: Use Interface
We create an interface called CapNhat contains methods capNhatGiaoDien(); GiaoDien then implements the following CapNhat:
package cachhoc.net.useinterface.senddata; public interface CapNhat { public void capNhatGiaoDien(int giaTri); }
package cachhoc.net.useinterface.senddata; public class Download { private CapNhat capNhat; /** * @param capNhat * lang nghe su kien download */ public void addDownloadListener(CapNhat capNhat) { this.capNhat = capNhat; } public void download() { for (int i = 0; i < 100; i++) { System.out.println("Dang download..."); /** * Khi goi capNhat.capNhatGiaoDien(i); thi phuong thuc nay se tu * dong duoc goi trong giao dien * */ capNhat.capNhatGiaoDien(i); } System.out.println("Ket thuc download"); } }
package cachhoc.net.useinterface.senddata; public class GiaoDien implements CapNhat { private Download download; /** * phuong thuc khoi tao Giao dien, dong thoi dat lang nghe su kien download */ public GiaoDien() { download = new Download(); /** dat lang nghe su kien download */ download.addDownloadListener(this); } private void nhanDownload() { download.download(); } /** * Tu dong duoc goi trong qua trinh download thong qua interface CapNhap */ @Override public void capNhatGiaoDien(int giaTri) { System.out.println("class giao dien cap nhat duoc: " + giaTri + "%"); } public static void main(String[] args) { GiaoDien giaoDien = new GiaoDien(); giaoDien.nhanDownload(); } }
You can see that the way 2 This overcomes the disadvantages of the classic way 1. Class does not need to download TV audience that just one interface GiaoDien CapNhat, therefore the method, calculation of non-drug GiaoDien “exposed” or may be referred to the Download. If you have passed a certain class X and then download the new implementation does not need to pass through the object GiaoDien intermediate class that has been updated interface during download. Or suppose there are many interfaces need to be updated during the download, it is indifferent as to how 1 would be more difficult because you need to pass all classes that interface to Download…
Above is 2 Interface applications they use most visible. We hope it can be helpful for you in the process learn how to use interface.
Source article at cachhoc.net
Natural light not understand how to use interface, search online blog that is very straight fall ku.
Pretty good article. And yet my message: “if you can supplement is something really great for me as well as you read tracking”, I'll comment 1 of this place.
1. Use Interface joint action to create objects: OK, I could use here interace, but could use more abstract class. So why do you use the interface instead of an abstract class?
2. Data transmission, messages between the class: excellent. Looking somewhat like the observer pattern, I've done 1/3. But he contributed more 2 standard for it to be 3/3 = 1 complete observer pattern.
2.1. Your Idea to GiaoDien implements good when CapNhat, and class work only with class Download CapNhat. But the working class directly GiaoDien with class Download? Why do not we do the same for Download, Download will mean implements 1 Certain InterfaceX, and will work with InterfaceX GiaoDien. The reason is similar implements GiaoDien CapNhat.
2.2. How if you wish for more classes notify Download 1 object CapNhat? Instead of using 1 object, I can use capNhat 1 Collection such.
OK, that's all. 🙂
Yes, Kids thank the comments of Mr. Hoang. You will affix to the article.
As for his question, the…
1. I think totally abstract class can do this, however, the due 1 class chỉ kế thừa được 1 class nên nếu A & B có hành động chung là Hab, B & C có hành động chung là Hbc, thế thì việc này sẽ khó… mà khi dùng interface sẽ ổn 😉
2.1 Em nghĩ cái anh đưa ra rất tốt và hay.
2.2 Dùng Collection trong trường hợp này là đúng là tiện nhất anh nhỉ 😉
Anh có thể viết lại 2 góp ý này được không ạ
cái interface phải đặt riêng 1 file khác à bạn? to the general public that their error it.
to the general well being that. If a simple interface for this common problem otherwise set apart for easy management.
I needed all my tutorials about OOP but then you just saw 2 share class , objects and interfaces. Hope you can guide all little rest!
Welcome. Now I wrote DL 2 it all alone… Other parts I have not had time to write you ah.
He explained why the interface for taking e abstraction 100% that abstract class from 0-100% OK
Since only interface functions abstract definition, they may also be deployed content
a can explain the meaning of the loop in the main function of the post is not it mangSachToiTruong help e?
for (sach s : clean) {
s.mangSachToiTruong();
}
That's foreach offline. Browse the list sach, each element s approval.
Thank you, very useful article!
Hello!
In part 2: Data transfer between the class, if he could explain more about the differences between 2 how not it? Now I see 2 interfaceface way whether or not, part capnhat(display) can still use the same, So different is nowhere sir?
Thank you!
However, many problems occur when the object is passed to giaoDien, download class is allowed to call a method of the class of which it is not necessary GiaoDien, updated interface methods required to allow calls in class Download it can be called that here alone to the public and not the private. Or suppose that the class we have to go through a certain class X in the middle of class, the method download Download new call and so we have to transfer to the X GiaoDien, X then transmitted to Download, so very complicated ... and a lot of other restrictions that when you do the actual will understand.
Tks him!
“However, many problems occur when the object is passed to giaoDien, download class is allowed to call a method of the class of which it is not necessary GiaoDien, updated interface methods required to allow calls in class Download it can be called that here alone to the public and not the private. ” : This place I have read and reread and still do not see differences between 2 from, when using the reference capnhat capnhat interface can still call methods in the same way giaodien 1, can still call the methods unnecessary :(.
In class you can call Download nhanDownload() ?. If the class can GiaoDien 1 public variable abc, you can call them directly abc variable in class through the other Download?
Xin lỗi đã làm phiền anh, em đã tìm ra chỗ sai của mình rồi! Thank you!
🙂 ko co chi 🙂
in the second example 2 the interface uses Is : the objects in class capnhat dowload it is likely a reference to any object of any class that implment it (capnhat like object represents all of the class object implements it , and after this you want 1 certain class as in class actions as dowload, I just have to not only implements sir capnhat ) , and in the interface method declaration does capnhat new object must call tel no sir ??
Can say so
Bài của anh hay quá,nếu có thể mong anh thêm nhiều ví dụ hơn về chủ đề này được không anh,
Thank you, mình sẽ cố gắng 🙂
Công dụng của interface còn gì nữa không anh? Mặc dù đã biết sơ qua về 2 công dụng trên nhưng xem 2 ví dụ của anh làm em thêm rõ hơn .Thank anh
Bạn có thể cho mình biết, khi nào dùng interface, khi nào dùng abstract
Na is used abstract vomiting when 1 class with the methods that can write out specific and also the methods not specifically written out. Interface used when all methods are not specific written off.
Let me ask how to let a class inherit information 2 different class sir? I have investigated and found that you should use interface?
Sme, In java there is no inheritance 2 normal class. which wants to inherit from 2 and above, use the interface.