Java does not have multiple inheritance, C + +, but Java provides interface, extends, and implement, the effect of multiple inheritance is still possible:
1 /**2 * Created by Franklin Yang on 2015.10.23.3 */4 5 //6 InterfaceBase {7 Public voidmethod1 ();8 }9 Ten InterfaceAIextendsbase{ One Public voidmethod2 (); A Public voidmethod3 (); - //Public void Method4 (); - //Public void Method5 (); the } - - InterfaceAIIextendsAI { - //Public void Method1 (); + //Public void Method2 (); - //Public void Method3 (); + Public voidmethod4 (); A Public voidmethod5 (); at } - - classAbaseImplementsbase{ - Public voidmethod1 () { -System.out.println ("Class abase implementation of interface base method 1"); - } in } - to classAbextendsAbaseImplementsai{ + //Public void Method1 () { - //System.out.println ("Class AB implements the interface Ai Method 1"); the // } * Public voidmethod2 () { $SYSTEM.OUT.PRINTLN ("Class AB implementation of interface Ai Method 2");Panax Notoginseng } - Public voidmethod3 () { theSYSTEM.OUT.PRINTLN ("Class AB implementation of interface Ai Method 3"); + } A the + //Public void Method4 () {} - //Public void Method5 () {} $ } $ - classADextendsAbImplementsaii{ - //Public void Method1 () { the //System.out.println ("class ad Implements interface Ai Method 1"); - // }Wuyi the - //Public void Method2 () {} Wu //Public void Method3 () {} - Public voidmethod4 () { AboutSYSTEM.OUT.PRINTLN ("class ad Implements interface aII Method 4"); $ } - Public voidMethod5 () { -SYSTEM.OUT.PRINTLN ("class ad Implements interface aII method 5"); - } A } + the //added by Whyang - classib{ $ Public voidDepend1 (Base b) { the b.method1 (); the } the } the - in classaa{ the Public voidDepend1 (AI i) { the i.method1 (); About } the Public voidDepend2 (AI i) { the i.method2 (); the } + Public voidDepend3 (AI i) { - i.method3 (); the }Bayi } the the classac{ - Public voidDepend1 (AII i) { - i.method1 (); the } the Public voidDepend2 (AII i) { the i.method2 (); the } - Public voidDepend3 (AII i) { the i.method3 (); the } the Public voidDepend4 (AII i) {94 i.method4 (); the } the Public voidDepend5 (AII i) { the i.method5 ();98 } About } - 101 102 Public classisolate2{103 Public Static voidMain (string[] args) {104System.out.println ("11111111"); theIB base =NewIB ();106Base.depend1 (NewAB ());107 108System.out.println ("22222222");109AA A =NewAA (); theA.depend1 (NewAB ());111A.depend2 (NewAB ()); theA.depend3 (NewAB ());113 theSystem.out.println ("33333333"); theAC C =NewAC (); theC.depend1 (NewAD ());117C.depend2 (NewAD ());118C.depend3 (NewAD ());119C.depend4 (NewAD ()); -C.depend5 (NewAD ());121 }122}
Run a run:
The 11111111 class Abase implements the interface base method 122222222 class Abase implements the interface base method 1 class AB implements the interface Ai Method 2 class AB implements the interface Ai Method 333333333 0
Java implements multiple inheritance: