Understanding of subclass overriding parent method in Java programming

The subclass re implements the method of the parent class; When rewriting, you can modify the access right modifier and return value, and the method name, parameter type and number cannot be modified; Only when the return value is a class type, the overridden method can modify the return value type, and must be a subclass of the return value of the parent method; Or it will not be modified, which is the same as the return value type of the parent class. So, how to understand? Why if the parent class returns a subclass of value type?

Ask the question: must a subclass override all methods of the parent class?

In Java, subclasses do not have to override all methods of the parent class. There are two cases:

When the parent method is an abstract method, the subclass must override (implement) all the abstract methods of the parent; when the parent method is an ordinary method, the subclass can override the parent method or not. For example:

Let's look at the example first. See below for details.

The package human defines three classes: person class, student class and testmain class. Student class is a subclass of person class. The codes are as follows:

The code of person class is as follows:

The student class overrides the override () method of the parent class. The code is as follows:

The code of testmain class is as follows:

The output result is:

Is there anyone like me who thinks the output of the first reaction should be "li Liu"? Why are both "Li"?

After careful analysis, just look at the following memory diagrams.

Statements 1 and 2 create a subclass object and a parent object respectively, where stu points to the subclass object and per points to the parent object. As shown in Figure 1 below:

Then execute the third statement: per = stu overRide();;

Stu first calls override (), creates a subclass object in the method body, and makes the temporary variable stu point to the object. Its storage location is the memory block with C as the first address;

Then assign the variable name of the object to "Li"; Finally, return the value of stu and assign it to per, that is, although per is the parent object reference, it finally points to the subclass object created in override (), which is represented by a blue arrow here; The parent object originally pointed to with B as the first address has no reference to it. Here, the red arrow is changed into a dotted line. At this time, access the name of per, which is obviously "Li". The memory structure is shown in Figure 2:

Then, execute per = per overRide();, Call the override () method;

Since the subclass overrides the override () method of the parent class, although per is referenced by the parent class object, the method of the parent class is overwritten, so the subclass method should be called at this time; The execution process is the same as above. Instead of pointing to the subclass object with C as the first address, per points to the newly created subclass object with D as the first address, as shown in Figure 3.

In the same way as above, the name of accessing per is still "Li" at this time, because the override() of the parent class has not been called at all twice.

Modify testmain as follows:

At this time, a parent class object is defined to refer to per2 and point it to the same object as per; In the last two lines, per2 calls the override () method, which obviously calls the method of the parent class. Therefore, the method body also creates the object of the parent class, and then returns the result to per2. At this time, per2 points to the newly created parent class object, and the name of the parent class object is "Liu".

Having said so much, it seems that the initial problem has not been solved. Why is it a subclass of the return value type of the parent class? For ease of illustration, remember that the return value type of the parent class is a.

My understanding is that this is for upward transformation; Since the subclass overrides the method of the parent class, sometimes it is necessary to call the method overridden by the subclass with the parent class object reference. In the case of the above example, that is, the subclass object reference of a should be assigned to the object reference of A. if the return value type is not class a or a subclass, the object reference of other classes cannot be assigned to the object reference of a, which will lead to an error; Therefore, if the return value of a method overridden by a subclass is a class type, its 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 don't know if it's clear.

PS: the example is not selected very well. If the return value type is a class irrelevant to person and student, it may be better understood. Otherwise, it is easy to confuse the class of the return value with the class of the method.

summary

The above is all about the understanding of subclass overriding parent method in Java programming. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Detailed explanation of static internal classes and code examples in Java

Object class of Java source code parsing

How to use the Java atomicinteger class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>