Java Features---polymorphism

Source: Internet
Author: User

1  Public classtestpolymorphic {2 3      Public Static voidMain (string[] args) {4 5         /*the characteristics of object-oriented polymorphism are divided into two kinds*/6         7         /*The first is a reference polymorphism, which means that the parent class can point to a reference to this class, or to a child class reference*/8Parent x =NewParent ();//point to this class reference9Parent s =NewChild ();Ten          One         /*But the subclass cannot point to the parent class reference*/ A         //Child C = new Parent (); //This will be an error . -          -         /*The 22nd is the method polymorphism, when creating this kind of object, called the method of the class, when creating the subclass object is called the subclass overrides or inherits the method*/ theX.test ();//The method in the parent class executes!!!  -S.test ();//The method in the subclass executes!!!  - S.fn (); -     } +  - } +  A classParent { at     Private intAge ; -     PrivateString name; -      Public intGetage () { -         returnAge ; -     } -      Public voidSetage (intAge ) { in          This. Age =Age ; -     } to      PublicString GetName () { +         returnname; -     } the      Public voidsetName (String name) { *          This. Name =name; $     }Panax Notoginseng      -      Public voidTest () { theSystem.out.println ("The method in the parent class has executed!!! "); +     } A      the      Public voidfn () { +System.out.println ("Subclass does not have this method, but it also executes because of inheritance.")); -     } $ } $  - classChildextendsParent { -      Public voidTest () { theSystem.out.println ("The method in the subclass executes!!!") "); -     }    Wuyi}

Java Features---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.