JAVA learning-Interface Usage method 2 (Inter-interface polymorphism), java Polymorphism

Source: Internet
Author: User

JAVA learning-Interface Usage method 2 (Inter-interface polymorphism), java Polymorphism

1 // There are also polymorphism 2 public class TestInterface {3 4 public static void main (String [] args) {5 Duck d = new Duck () between the interface and the specific implementation class (); 6 TestInterface. test1 (d); 7 TestInterface. test2 (d); 8 TestInterface. test3 (d); 9} 10 11 public static void test1 (Runner r) {// Runner r = new Duck (); 12 r. run (); // virtual method call 13} 14 public static void test2 (timer mer s) {// timer mer s = new Duck (); 15 s. swim (); 16} 17 public static void test3 (Fli Er f) {// Flier f = new Duck (); 18 f. fly (); 19} 20} 21 22 interface Runner {23 public abstract void run (); 24} 25 interface timer mer {26 void swim (); 27} 28 interface Flier {29 void fly (); 30} 31 32 class Duck implements Runner, Timer mer, Flier {33 34 @ Override35 public void fly () {36 System. out. println ("The Ugly Duckling can also become a white swan! "); 37} 38 39 @ Override40 public void swim () {41 System. out. println (""); 42} 43 44 @ Override45 public void run () {46 System. out. println ("duck ass twisting"); 47} 48 49}

 

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.