Java up and down transformation

Source: Internet
Author: User

Transformation is based on inheritance, which is a mechanism of code reuse in object-oriented languages, through inheritance, subclasses can reuse the function of the parent class, and if the parent class does not meet the requirements of the current subclass, the subclass can override the methods in the parent class to extend it.

Transition up: The object that the subclass refers to is converted to a parent class type called upward transformation. In layman's words, a subclass object is converted to a parent class object. Here the parent object can be an interface

Downward transformation: The parent class references an object that is converted to a subclass type called downward transformation.

The former is an upward transformation, the Animal Dog reference points to New dog (), the subclass object as a parent object, only the members of the parent class, if the subclass overrides the method of the parent class, according to this reference to the calling subclass override this method (this method overrides override) . This calling procedure is called dynamic binding.

Issues to be aware of in transition:

When you move up, the parent class points to the subclass reference object and loses other methods that are common to the parent object, that is, during the transformation, the new methods of the subclass are lost, and at compile time, the system provides an error that the method cannot be found. Examples are as follows:

1  PackageCom.hh.Object;2 3 classAnimal {4 5      Public voideat () {6 7System.out.println ("Animal eatting ...");8 9     }Ten  One } A  - classBirdextendsAnimal { -  the      Public voideat () { -  -System.out.println ("Bird eatting ..."); -  +     } -  +      Public voidFly () { A  atSystem.out.println ("Bird flying ..."); -  -     } -  - } -  in  Public classMain { -  to      Public Static voidMain (string[] args) { +  -Animal B =NewBird ();//Upward Transformation the  * b.eat (); $ Panax NotoginsengB.fly ();//It is suggested here that the Fly method is not defined in animal.  -     } the}
View Code

In the downward transformation process, there are two situations:

Case one: If the object referenced by the parent class refers to a sub-class object that is pointed to, it is safe in the process of downward transformation. That is, compiling does not make mistakes.

Scenario Two: If the parent class refers to an object that is the parent class itself, it is unsafe in the process of downward transformation and compiles without errors, but java.lang.ClassCastException errors occur at run time. It can use instanceof to avoid errors like this. Examples are as follows:

1  PackageCom.hh.Object;2 3 classGirl {4      Public voidSmile () {5System.out.println ("Girl Smile () ...");6     }7 }8 9 classMmgirlextendsGirl {Ten @Override One      Public voidSmile () { ASYSTEM.OUT.PRINTLN ("Mmirl smile sounds sweet ..."); -     } -  the      Public voidC () { -System.out.println ("Mmirl C () ..."); -     } - } +  -  Public classMain1 { +  A      Public Static voidMain (string[] args) { atGirl G1 =NewMmgirl ();//Upward Transformation - g1.smile (); -Mmgirl MMG = (mmgirl) G1;//down transformation, compile and run without errors - mmg.smile (); - mmg.c (); -Girl g2 =NewGirl (); in          -         //mmgirl mmg1 = (mmgirl) G2;//unsafe down-conversion, compile error-free but run errors to         //mmg1.smile (); +         //mmg1.c (); -          the         if(G2instanceofmmgirl) { *Mmgirl MMG1 =(Mmgirl) G2; $ mmg1.smile ();Panax Notoginseng mmg1.c (); -         } the  +     } A}
View Code

Summarize:

1. The parent class reference can point to the subclass object, and the subclass reference cannot point to the parent class object.

2, the object of the class directly assigned to the parent class reference called upcasting upward transformation, upward transformation without forced transformation.

such as Father father = new Son ();

3. assign the parent reference of the child class object to the subclass reference called down Transformation (downcasting), to force the transformation.

If father is a parent reference to a subclass object, assign father to the subclass reference son, son son = (son) father;

Where the Father Front (Son) must be added for casting.

4, Upcasting will lose the subclass-specific method, but the subclass override the parent class method, the subclass method is valid

5, the role of upward transformation, reduce duplication of code, the parent class as parameters, and sometimes sub-class as a parameter, is the use of upward transformation. This makes the code concise. Embodies the idea of Java abstract programming.

Java up and down transformation

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.