1 //There is also polymorphism between the interface and the specific implementation class2 Public classTestInterface {3 4 Public Static voidMain (string[] args) {5Duck d =NewDuck ();6 Testinterface.test1 (d);7 Testinterface.test2 (d);8 Testinterface.test3 (d);9 }Ten One Public Static voidTest1 (Runner R) {//Runner r = new Duck (); AR.run ();//virtual method Invocation - } - Public Static voidTest2 (Swimmer s) {//Swimmer s = new Duck (); the S.swim (); - } - Public Static voidTest3 (flier f) {//flier F = new Duck (); - f.fly (); + } - } + A Interfacerunner{ at Public Abstract voidrun (); - } - Interfaceswimmer{ - voidswim (); - } - Interfaceflier{ in voidfly (); - } to + classDuckImplementsrunner,swimmer,flier{ - the @Override * Public voidFly () { $System.out.println ("Ugly duckling can also become a white swan!") ");Panax Notoginseng } - the @Override + Public voidswim () { ASystem.out.println ("Red Palm dial Clear Wave"); the } + - @Override $ Public voidrun () { $System.out.println ("The duck's hip twist Walk"); - } - the}
Java Learning-Interface usage Method 2 (inter-interface polymorphism)