"JAVA" for upward transformation and downward transformation

Source: Internet
Author: User

Upward transformation:

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

1 Public class Animal {2Publicvoideat () {3System.out.println ("Animal eat!");4          }5 }6 7 class Bird extends animal{8Publicvoideat () {9System.out.println ("Bird eat!");Ten          } One  APublicvoidFly () { -System.out.println ("Bird fly!"); -          } the } -  - class main{ -public staticvoidMain (string[] args) { +Animal B =NewBird ();//Upward Transformation - b.eat (); +B.fly ();//It is suggested here that the Fly method is not defined in animal.  A } at         

In the above example, Animal B = new Bird () is a typical upward transformation, the subclass object as the parent class object, only the members of the parent class can be called, if the subclass overrides the method of the parent class according to this reference to call the subclass override this method is overridden. This calling procedure is called dynamic binding.

It is important to note that when you move up, the parent class points to the subclass reference object and loses the other methods that are common to the parent object, that is, the new methods of the subclass are lost during the transformation, and the system provides errors that cannot be found at compile time. In the example above, B.fly () is the cause of the error.

Downward transformation:

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

1 Public class Person {2PublicvoidSmile () {3System.out.println ("Person smile!");4     }5 }6 7 class Girl extends person{8PublicvoidSmile () {9System.out.println ("Girl smile!");Ten     } One  APublicvoidSing () { -System.out.println ("Girl sing!"); -     } the } -  - class main{ -public staticvoidMain (string[] args) { +Person person=NewGirl ();//Upward Transformation - person.smile (); +  AGirl girl= (Girl) person;//down transformation, compile and run without errors at girl.smile (); - girl.sing (); -  -Person person1=NewPerson (); -         //Girl girl1= (Girl) Person1;//unsafe down-conversion, compile error-free but run errors -         //girl1.smile (); in         //girl1.sing (); -  to         if(Person1instanceofGirl) { +Girl girl1=(Girl) Person1; - girl1.smile (); the girl1.sing (); *         } $}

In the downward transformation process, there are two situations:

Case one: If the object referenced by the parent 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.

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 target class object directly to the parent class reference called 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, 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, the upward transformation will lose the subclass unique method, but the subclass overrides 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.

"JAVA" for upward transformation and downward transformation

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.