The upward transformation and downward transformation of Java SE

Source: Internet
Author: User
Tags java se

 Packageobject;//shift up-down transformation Public classUp_down_convert {/*Move up * 1. On the transformation object can use and manipulate subclasses to inherit or override the method * 2. The action and use of "new member" or "new method" of the child class is lost on the previous transformation*/     Public voidUpconvert () {Father father_a=NewSon ();        Father_a.say ();    Father_a.action (); }        /*Downward Transformation * 1. Assign the parent class reference to the subclass object to the subclass reference, which is called down transformation * 2. To cast * 3. A down-transition instance can regain the method lost due to an upward transformation (subclass exclusive) **/     Public voidDownconvert () {Father father_a=NewSon ();//Move up first (go to Father's Class)Father_a = (Son) father_a;//Error: Further down (forced) transformationFather_a.say (); Father_a.action ();//Father_a.getmoney ();//This method (subclass exclusive) cannot be usedSystem.out.print ("**************************\n"); Father Father_b=NewSon ();//Move up first (go to Father's Class)Son son = (son) father_b;//Right : Another new declaration of a son object, then a downward transformation instantiationSon.say ();        Son.action (); Son.getmoney ();//after the transition, this method still (subclass exclusive) can "resume using"    }     Public Static voidMain (String args[]) {NewUp_down_convert (). Upconvert ();//Upward Transformation//output: I'm a son! Son go to school!System.out.print ("**************************\n"); NewUp_down_convert (). Downconvert ();//Down Transformation    }}classfather{ Public voidsay () {System.out.print ("I'm a father!"); }     Public voidaction () {System.out.println ("Father goes to work!"); }}classSonextendsfather{ Public voidsay () {System.out.print ("I'm a son!"); }     Public voidaction () {System.out.println ("Son goes to school!"); }         Public voidGetmoney () {System.out.print ("Son wants living expenses!" "); }}

Output

I'm a son! Son goes to school! I'm a son ! Son goes to school! I'm a son ! Son goes to school!

The upward transformation and downward transformation of Java SE

Related Article

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.