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