Java study note 13 --- How to understand "when a subclass overrides the parent class method, if the returned value is of the class type, it must be of the same type or be its subclass as the returned value of the parent class", study note 13 ---

Source: Internet
Author: User

Java study note 13 --- How to understand "when a subclass overrides the parent class method, if the returned value is of the class type, it must be of the same type or be its subclass as the returned value of the parent class", study note 13 ---

The sub-class implements the method weighing and writing of the parent class again. The access permission modifier and return value can be modified during rewriting. The method name and parameter type and number cannot be modified. Only when the return value is of the class type, the type of the returned value must be a subclass of the returned value of the parent class method, or the type of the returned value is the same as that of the parent class. So how can we understand it? Why is it a subclass of the return value type of the parent class?

Let's take a look at the example first. For more information, see the following.

The human package defines three classes: the Person class, the Student class, And the TestMain class. The Student class is a subclass of the Person class. The Code is as follows:

The Person class code is as follows:

Package human; public class Person {String name; int age; // test: overRide public Person overRide () {Person per = new Person (); per. name = "liu"; return per ;}}

 

The Student class overrides the overRide () method of the parent class. The Code is as follows:

Package human; public class Student extends Person {String stuNumber; int score; // test: overRide public Student overRide () {Student stu = new Student (); stu. name = "li"; return stu ;}}

 

The code of the TestMain class is as follows:

package human;public class TestMain {    public static void main(String[] args) {        Student stu = new Student();        Person per = new Person();        per = stu.overRide();        System.out.println(per.name);        per = per.overRide();        System.out.println(per.name);}

 

Output result:

1 li2 li
View Code

Is there anyone like me that the first response should be "li liu"? Why are both "li "?

After careful analysis, you can see the following memory diagrams.

1st and 2nd statements respectively create a subclass object and a parent class object. stu points to the subclass object and per points to the parent class object. As shown in figure 1 below:

Figure 1

 

Then execute the following 3rd statements: per = stu. overRide ();;

Stu first calls overRide (), creates a subclass object in the method body, and points the Temporary Variable stu to the object. The storage location is the memory block with the C-headed address;

Then assign the variable name of this object to "li". Finally, return the stu value and assign it to per. That is to say, although per is referenced by the parent class object, it finally points to overRide () the Child class object created in, which is represented by a blue arrow. The parent class object pointing to B is not referenced to point to it, and the Red Arrow is changed to a dotted line. The name of the access per is obviously "li ". The memory structure is shown in Figure 2:

 

Figure 2

 

Then execute per = per. overRide (); and call the overRide () method;

Because the subclass overrides the overRide () method of the parent class, although per is referenced by the parent class object, this method of the parent class is overwritten, so the subclass method must be called at this time. The execution process is the same as above, per no longer points to the subclass object with the C-headed address. Instead, it points to the newly created subclass object with the D-headed address, as shown in 3.

In the same way as above, the name of the access per is still "li" because the overRide () of the parent class is not called twice.

Figure 3

 

Modify TestMain as follows:

package human;public class TestMain {    public static void main(String[] args) {        Student stu = new Student();        Person per = new Person();        Person per2 = per;//        per = stu.overRide();        System.out.println(per.name);        per = per.overRide();        System.out.println(per.name);        per2 = per2.overRide();//        System.out.println(per2.name);//}

At this time, a parent class object is defined to reference per2 and point it to the same object as per. In the last two rows, per2 calls the overRide () method, obviously, the method of the parent class needs to be called. Therefore, the object created in the method body is the parent class object, and the result is returned to per2. At this time, per2 points to the newly created parent class object, the name of the parent class object is "liu.

Having said so much, it seems that the problem at the beginning has not been solved. Why is it a subclass of the return type of the parent class? For convenience, the return value type of the parent class is.

In my understanding, this is for upward transformation. Since the subclass overrides the parent class method, sometimes it is necessary to use the parent class object reference to call the override method of the subclass, in the case of the above example, that is to say, we need to reference the subclass object of a to the object of A. If the return value type is not A class or A subclass, the object references of other classes cannot be referenced by the object assigned to A, which will lead to errors. Therefore, if the return value of the subclass override method is of the class type, the return value type must be the same as the return value type of the parent class or a subclass of the return value type of the parent class.

I wonder if I have made it clear.

PS: the example is not particularly well selected. If the returned value type is a class that the Person and Student do not want to do, it may be better understood. Otherwise, it is easy to confuse the returned class with the class to which the method belongs.

 

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.