The upward transformation and downward transformation of Java

Source: Internet
Author: User

Just beginning to touch the Java transformation, although now do not know why to transform, but first memorize the grammar bar

Upward transformation:

First you have a parent class, a subclass,

Person P=new person ();

P=new Student ();

OK, this is the upward transformation that can be simplified into: person p=new Student ();

1) p is a reference to the person, pointing to the Student object, p is not an object;

2) P can only invoke member variables and member functions in the parent class, the new method in the subclass and the variable p cannot be used, and the body being executed is the body of the subclass.

such as Class person{

void Eat () {System. OUT.PRINTLN ("Eating ...")}

void Sleep () {System.out.println ("Sleep ...")}

}

Class Student extends person{

void Eat () {System.out.println ("I am a Student")}

void Read () {System.out.println ("I am Reading ...")}

}

P can call the Eat method, execute the function body in the student, or call the sleep method, there is no sleep method in the subclass, so the function body in the parent class is executed;

However, the Read method cannot be called because the person does not have the Read method;

Downward transformation:

A downward transition is a line of upward transformation

Student s= (Student) p;

Likewise S is not an object, it is a reference

  S can call all methods in the parent class and subclass, execute the function body in the subclass, do not have in the subclass, then execute the function body in the parent class

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 methods, but subclasses overriding the method of the parent class, the subclass method is valid

I do not know whether there are errors, even if there are errors now can not find, learn slowly, so

The upward transformation and downward transformation of Java

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.