Java Learning-Interface usage Method 2 (inter-interface polymorphism)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.