本文发表在 rolia.net/zh 相约加拿大网上社区枫下论坛
Recently, some friends are discussing the differences between abstract classes and interfaces. It seems to me this is not a right theme. Of course, there are some common points and differences between them,
at least TECHINCALLY.
I understand we should always ask the following the question: class or interface? As we know, abstract classes are just one special type of classes.
Here are some points:
1) If you just want to define an API and leave the implementation open,
please use interface. A good example is Servlet API, in which HttpRequest and HttpReponse are interfaces. Vendors can implement
HttpRequest and HttpResponse in different way, but their different implementations look the same to us application programmers.
2) Interfaces are used to provide some specific data types. For instance,
JDK has a java.io.Serializable interface to distinguish those Objects that can be stored in a file, or transferred on the wire.
3) In some Java API, the class heirarchy has been established by the Framework----Java RMI and EJB are good examples. In such a case, even our business concepts need to be put into interfaces. If you have
ever done some RMI and EJB programming, you will agree with me. Take RMI as an example. If you want to implement a "remote" Person,
Person must be an interface because Remote Person Server Implementation must extends a super class such as UniCastRemoteObject. Likely, if you want to do a Calculator Applet or a Person Applet, you may bring in the functionality of Calculator or Person by defining corresponding interfaces. Put it in another word, in Java Application you may define an Animal class, but in RMI and EJB you have to define it as an interface! This is the answer, bellieve or not.
4) Interfaces can host some contants. This is a nice unitilty. However, it is a minor in the whole business.
5) A lot of people say that interfaces can be used to host some un-implemented methods. Here, they just see the trees instead of the forest. Can I do the following stupid thing? ----Define an interface, and
put two DISPARATE method signatures there: One is "public void actionPerformed(ActionEvent e)", and another is "public void sing(String song)". I believe you will say NO. The true usage of interface is to difine
some data type, or some roles in the business. For instance, if you define a Programmer interface, then it should contain methods signatures decribing the role of a Programmer. Talking about individual method signatures do not make much sensense. We should talk about Programmer. Somebody else way write a method, WHICH CAN ACCEPT Programmer as INPUT ARGUMENTS. This is what data type means!
5) Again, please don't fuss about abstract class and interface. At the very beginning, don't think about abstract class because it is only a special type of Java classes.
6) A java programmer should be proficient in making such decisions about using classes and interfaces. At the very beginning, I was confused a lot. As time goes, thing become more and more transperen to me. I hope this will be the case to all the Java people in this forum.
更多精彩文章及讨论，请光临枫下论坛. 网址: rolia.net/zh