Java Study Notes 15 --- instanceof and downward transformation,

Source: Internet
Author: User

Java Study Notes 15 --- instanceof and downward transformation,

The cold and cough stopped for a few more days, and the update is resumed today.

Let's take a look at the concepts of instanceof and downward Transformation:

1. instanceof

Instanceof is a binary operator. Its usage is:Boolean result = a instanceof ClassA,Determines whether object a is an instance of the ClassA class. If yes, true is returned; otherwise, false is returned.

 

2. Downward Transformation

Note 12 has already explained the upward transition, that is, the parent class Object references the Child class object;Downward transformation means that the object reference of the subclass is assigned as a parent class object reference, and an explicit forced type conversion is required when the value is assigned, the reference of this parent class object must point to the subclass object. In the end, the reference of the subclass object directs to the subclass object. That is to say, after the upward transformation, it will be changed back through the downward transformation; the lost attributes and methods after the upward transformation have gone through the downward transformation. For the significance of the downward transformation, see http://blog.csdn.net/xyh269/article/details/52231944.

It is easy to use text to describe and understand it. Remember "Person" as the parent class, and "Student" as the subclass.

Person per = new Student (); // upward Transformation

Student stu = (Student) per; // downward Transformation

Since per originally points to a subclass object, stu can also point to this subclass object through explicit type conversion.

 

The following statements cannot be transformed downward:

Person per1 = new Person ();

Student stu1 = (Student) per1;

The following error occurs:

Exception in thread "main" java. lang. ClassCastException: human. Person cannot be cast to human. Student
At human. TestMain. main (TestMain. java: 15)

Because per1 points to the parent class object, stu1 is a subclass reference, and the number of attributes and methods of the subclass is greater than that of the parent class, it cannot be converted to a subclass object.

The following memory diagram is more intuitive:

 

3. How can we determine whether a downward transformation is possible?

You can first use instanceof to determine whether the object reference of the parent class points to the subclass instance. If yes, you can perform a downward transformation. Otherwise, you cannot.

 

4. The following is an example code:

Package human; public class TestMain {public static void main (String [] args) {Person per = new Person (); Person per2; Student stu = new Student (); Student stu2; dustMan dus = new DustMan (); per2 = stu; stu2 = (Student) per2; // stu2 = (Student) per; // stu2 = (Student) dus; if (per2 instanceof Student) System. out. println ("per2 points to the object of the Student Class"); else System. out. println ("per2 is not a Student class Object"); if (per2 instanceof Person) System. out. println ("per2 points to the Person Class Object"); else System. out. println ("per2 is not the object of the Person class"); if (per instanceof Student) System. out. println ("per points to the objects of the Student Class"); else System. out. println ("per points to an object not of the Student Class"); if (per instanceof Person) System. out. println ("per points to the Person Class Object"); else System. out. println ("per points to an object not of the Person class"); if (stu2 instanceof Student) System. out. println ("stu2 points to the Student Class Object"); else System. out. println ("stu2 points to an object not of the Student Class"); if (stu2 instanceof Person) System. out. println ("stu2 points to the Person Class Object"); else System. out. println ("stu2 points to an object not of the Person class"); if (stu instanceof Student) System. out. println ("stu points to the Student Class Object"); else System. out. println ("stu points to not the Student Class Object"); if (stu instanceof Person) System. out. println ("stu points to the Person Class Object"); else System. out. println ("stu is not a Person Class Object ");}}

The output result is as follows:

1 per2 points to the Student Class Object 2 per2 points to the Person Class Object 3 per points to not the Student Class Object 4 per points to the Person Class Object 5 stu2 points student Class Object 6 stu2 points to the Person Class Object 7 stu points to the Student Class Object 8 stu points to the Person Class Object

Result Analysis:

(1). per2 = stu;

Stu2 = (Student) per2;

Per2 and stu point to the same subclass object, so the following statements can be transformed downward.

(2). // stu2 = (Student) per;

As per points to the object of the Person class, downward transformation will fail.

(3). From the output of rows 1, 2, 5, 6, 7, and 8, we can see that,When instanceof is used, no matter whether a is defined as the object reference of the parent class or the object reference of the subclass, as long as it finally points to the subclass object, instanceof determines that it is both the instance of the parent class and the instance of the subclass.;That is, the subclass instance contains a parent class instance.

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.