According to DDJ's C / C columnist Al Steven said: he does not understand Java, but seeing these books to interpret the Concept of C objects, some places are obviously wrong, it is really worrying. This article assumes that you are familiar with some concepts of C / C languages, and Java has a preliminary understanding. And talk about the main difference between Java's Interface and C 's multiple inheritance. Interface and multiple inheritance ideas regardless of Java's Interface or C multiple inheritance, in the theory of object-oriented theory, it is a very novel concept. So here we talk about the perspective of programming language, see all the meaning and functionality of Java Interface, whether C multi-inheritance can all interpret? Or instead of interpret C in Java. Let us review what is C multiple inheritance. "Inherit" is usually auster to play a re-exploit of a program code, while C multi-inheritance makes a subcategory to inherit many parent categories that belong to different data types, as follows: #include < STDIO.H> Class test1 {public: Virtual void f1 () {PUTS ("TEST1:: F1 ()");} Virtual Void G1 () {PUTS ("TEST1:: G1 ()");}}; Test2 {public: Virtual Void F2 () {PUTS ("Test2 :: F2 ()");} Virtual Void G2 () {PUTS ("Test2 :: g2 ()");}}; Class Test3: Public Test1, PUBLIC TEST2 {PUBLIC: Virtual Void GG () {PUTS ("TEST3 :: GG ()");}}; void main () {TEST3 T3; T3.F1 (); t3.f2 (); t3.g1 ( ); T3.G2 (); T3.GG ();} // program output: test1 :: f1 () test2 :: f2 () test1 :: g1 () test2 :: g2 () TEST3 :: gg () Multiple inheritance of Cheng Form 1, C According to [RIE96], it is considered that the "multiple inheritance" concepts in object-oriented technology should be used. It should be as follows: Suppose there is a wooden door, then: 1. This wooden door is a door (a Kind of). 2. But the door is not a part of the wooden door (a part of). 3. Wooden door is a kind of wood product. 4. However, wood products are not part of the wooden door. 5. Wood products are not a door. 6. The door is not a kind of wood product. So you can find that multiple inheritance must be very careful, and in many times, we don't need multiple inheritance. Java also provides inheritance mechanisms, but also provides a concept called Interface. Since Java's inheritance mechanism can only provide a single inheritance (just inherited a parent category), it will replace C multiple inheritance with Java's interfab. Interface is a two items that are regulatively communicated, which are regulations for communications. As shown in Java program, Java's Interface indicates: some functions or information members, and they need them to share these functions, and they are defined in an interface, and then Let all of the different categories of Java objects can be used together.
So, for Java inheritance and interface, we summarize as follows: 1. Java's class can only inherit a parent category (with an Extends keyword), but you can have (or actually) Many interface (with the imports keyword). 2. Java's Interface can inherit many other Interface (also with an Extends keyword), but not to do any interfab. Therefore, we can use Java's Interface to simulate multiple inheritance of C . As described above, it can be converted as follows: Interface test1 {public void f1 (); public void g1 ();} interface test2 {public void f2 (); public void g2 ();} interface test3 extends test1, test2 {public void gg ();} CTEST IMPLEMENTS TEST3 {public void f1 () {system.out.println ("TEST1 :: F1 ()");} public void g1 () {system.out.println ("TEST1 :: g1 ) ")");} Public void f2 () {system.out.println ("TEST2 :: F2 ()");} public void g2 () {system.out.println ("TEST2 :: G2 ()"); } Public void gg () {system.out.println ("TEST3 :: gg ()");}} CTEST (); CT.F1 (); CT. F2 (); ct.g1 (); ct.g2 (); ct.gg ();}} class main {public static void main (string args []) {Run rr = new run (); r.Run );}} // Program output: TEST1 :: F1 () TEST2 :: F2 () TEST1 :: G1 () TEST2 :: G2 () TEST3 :: GG () C multiplex with Java's Interface Inheritance function However, according to [ait96], he thinks that Java's Interface is much better than C multiple inherits, but it is easier to understand, but there is a limit. For Java Interface, it is not explained in the article. It is mainly the difference in "intervening inheritance" and "inheritance" concept. "Interface inheritance" is only the function name of the parent category, and then the subcategory will definitely replace it. So when we use the "Multi-Type" in the individual category, since the subcategory will do the multi-type function of the parent category, each subcategory is different, at this time we (using the parent category) The person of the indicator) does not know how this multi-function is completed, and it is called "black box design". "Inheritance" is the function name of inheriting the parent category, and the subcategory will also use the function of the parent category when it works. So we (those who use parent category indicators) know how this multi-function is completed, because it is similar to the function of the parent category, because it is called "white box design".
Suitable Java's Interface, we found that Java's Interface is the interface inheritance, because Java Interface can only define function names, unable to define functions, so subcategories must be implemented with "imports" keywords, and each Subcords in the same interface, of course, don't know how the other party is actually, so it is a black box design. Java's category inheritance is inheritance, and subcategories will use the parent category (more correctly, the parent category is defined, so the subcategories may be used, even if it is not used, will follow the father Category The actual algorithm), so the parent category has a certain degree of correlation with the subcategory; unlike the interface inheritance, only the function name is just the same as the mean. Interface inheritance and inheritance, it is easy to understand its integration with the INTERFACE, CLASS, EXTENDS and Implements keywords to Java. But the inheritance mechanism of C seems to be not so easy to explain! So this is the meaning of [ait86] in the article: C multi-mechanism is more complicated. So next we will discuss: What is the function of C multiple inherits, is the Java's Interface? In C ARM, or [STR94], the following famous examples are mentioned: #include
C virtual functions, you can refer to [Sou94], C compiler for each virtual function, and establish a virtual function table and it should be, because each virtual function may have many subcategories in one inherited tree. So in actual execution, you are looking for a virtual function table first, then find the function of the most close to your class class level. So we can convert a parent category indicator to a child category, but still perform the virtual function of the mother. Java does not perform the above functions through Interface. Java's abstract function (ABSTRACT FUNCTION) is actually a pure virtual function of C (Virtual function () = 0), without a non-pure virtual function like C (it is a virtual function to be defined), so it is difficult Examples of complex above. Also, the function of the Java's interface is preset to abstract functions, that is, if a category is actually this interface, all functions in the interface must be all active. Therefore, in actual execution, first determine the INTERFACE object into a certain type of object, then the function within this category is executed. At first glance, it seems that C 's multiple inheritance features are complete, then: Java's Interface concept, is it possible to use C multiple inheritance simulation? Currently, in Microsoft's OLE technology, it is indeed the interface concept in the MELCE in the MFC in Multi-inheritance analog OLE, and you can refer to the writing of OLE COM in the MFC. The following program: class CoSomeObject: public Iunknown, public Ipersiet {// IUnknown methods virtual DWORD AddRef (void); virtual DWORD Release (void); virtual HRESULT QueryInterface (REFILD, LPVOID FAR *); // IPersist methods virtual HRESULT GetCLASSID (LPCLSID PCLSID);}; (Note: See MFC INTERNALS PP 442) However, considering the following problems: Java Interface features, interfab can be inherited. But here's so-called inheritance, in fact, the subinterface includes members in all parent Interface, which can be replaced by new definitions of subcategories like Java categories or C categories. This is the difference between the so-called "interface inheritance" and "inheritance". In other words, Java Interface inherits (note that odds of subcategory "is just a" inclusive relationship ", or even inclusion cannot be repeated. Therefore, the interface concept is very powerful in the interface between the soft modules, such as the IDL of CORBA. Therefore, Java Interface still has many advantages, but how if we want to simulate the multiple inheritance simulation of C (or known to learn the Java Interface concept)? We mainly follow the Java programs, observe the C simulation interface, how to get the right.