java--Overloading and rewriting

Source: Internet
Author: User

parent Class (parental): Public classParent { PublicString name = "Parent Parental class Property";  Public voidsay () {System.out.println ("Parent say is called" + This. GetClass ()); }} subclass (Child): Public classChildextendsParent {/*** Attributes can only be hidden and cannot be overwritten*/     PublicString name = "Child sub-class attribute";  Public Static voidMain (string[] args) {child C=NewChild ();//Normal ObjectC.say (); Parent P=NewChild ();//Upward TransformationP.say (); /*** When subclass child and parent have the name attribute: * c.name = Child Subclass Property * P.name = Parent Class Property * When Subclass child Without the Name property, when the parent class has the property: * c.name = Parent property of the parents * P.name = Parent Property*/System.out.println ("C.name =" + c.name);//c.name = ChildSystem.out.println ("p.name =" + p.name);//P.name = parent            }        /*** Overriding parent class methods*/     Public voidSay () {//Subclass Method OneSystem.out.println ("Child say is called" + This. GetClass ()); }        /*** Overloading--subclass Method One * When the subclass method does not exist, the subclass Method II looks like the programmer intentionally overwrites the function of say (), but the type definition of the method is slightly imprecise.     * However, the compiler now assumes that the programmer is intentionally "overloaded" rather than "overwritten." */     Public voidSayints) {//Subclass Method TwoSystem.out.println ("Child say is called" + This. GetClass ()); }        /*** * method signature consists of the method name and a list of parameters (the order and type of the parameters of the method), which is called overloading when defining two or more methods with the same name.     * The parameter list for the same name member is not the same when overloaded (parameter order and type). * The following say2 three methods are the embodiment of overloading*/     Public voidSay2 () {//Subclass Method ThreeSystem.out.println ("Child say2 is called"); }         Public voidSay2 (intA) {//Subclass Method FourSystem.out.println ("Child say2 is called"); }         Public intSay2 (String a) {//Subclass Method FiveSystem.out.println ("Child say2 is called"); return0; }}

java--Overloading and rewriting

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.