Preliminary understanding of java--eighth-inheritance-member functions in child-parent classes-Overrides

Source: Internet
Author: User

One.

Talk about the completion variables and start talking about the characteristics of the member functions in the child parent class. (also review the differences between the different functions, and why is there such a difference?) )

Subclasses inherit the parent class and are not private, and can be used directly.

This paper explains the operation process of the program in-memory.

First the main function main and the reference variable Z are loaded into the stack, and then the object of the Zi class is created, because the Zi class inherits the Fu class, so the parent class is loaded first. The class is loaded into the method area, first loading the constructor, and then loading the Show1 () method. Then load the Zi class, which is also loaded into the constructor Zi (), General function Show2 (), because there is an inheritance relationship, there is a super keyword that points to the parent class.

Immediately after, the heap in memory new A Zi class object, after the object is established, assigns the address number to the reference variable z, completes the z point to the object.

The next step is to execute Z.show1 () and Z.show2 (), the object invocation method. Since the object is created according to the Zi class, Z.show1 (), when executed, is looking for a show1 () method in the subclass, and must first find himself. Look, no, next, follow the super to find the parent class, find Show1 (), Show1 () into the stack, and then run. To the third sentence, the object calls the Show2 () method, the method is found in the subclass, and the stack runs to the end. → Here you also want to insert a question about the inclusion of the subclass content and the parent class content . This problem is analyzed from two angles, from the code point of view, according to the inheritance relationship between the two classes, the content is completely different (from the definition of inheritance, the parent class is extracted from the subclass, after extraction, the sub-class does not exist in the content of the part, this part of the content is a single class). From the back of the meaning, although the contents of the two classes are completely different, but the subclass contains the parent class.

Above is a description of the general situation, we have to focus on the special situation. That is, two classes have the same name in the method. According to our previous knowledge, there are two classes in a class that cannot have the same declaration, and there will be an issue with an indeterminate invocation. However, there can be two functions of the same declaration in the child parent class, why, because they belong (although the declaration is the same, but the location of the store is different, then the two functions are different).

As shown, if the EXECUTE statement calls the show () method, because the subclass has a show (), then the direct call is possible, no need to go to the parent class to look for.

(These two attributes have only functions, and the overloaded definitions are forgotten .) )

Only subclasses can overwrite the parent class.

Coverage means that, as if the method of the parent class is overwritten, there is no overlay and looks like.

In addition , it should be said that not every time the object is created, it is a subclass that is used as the object's description class. Or, because it is a fixed inheritance relationship, according to the previous analysis of the collation, it is guaranteed that the subclass is containing the largest range of content, then each creation of the object is a subclass.

Considerations for overriding operations :

It must be shown that the subclass and the parent class are exactly the same, (as is the case here), the function is said to return a value type, a function name and a parameter list, which is the declaration of functions consistent. (and overloading is what separates the parentheses)

What does that mean?

At present we have learned three kinds of permissions, public,private, and nothing to write the default permissions (before what permission modifier is not written, itself is a kind of permission). When we cover it, it is possible to have the same permissions. If there is public in the parent class and nothing in the subclass, then the permissions of the subclass are lower than the parent class, which is inappropriate ( what is the default permission size ?). ), cannot be covered like this. The DOS results are shown below, meaning that the permissions for subclasses are too low. This is what is said about insufficient coverage rights,

This is equal if the permissions of the child parent class are public. If the function in the parent class is the default permission, the child class is public permission, and compilation is not a problem.

If private permissions are in the parent class, the child class is the public permission. This is not called coverage, why? Overlay has such a feature, that is, the parent class has a I can use, but I build my own, so compile an identical, cover you up, with my own, but here in the parent class this is not used. Private, outside do not know, sub-class at all do not know the parent class has, do not know what to talk about covering it.

What does it mean to overwrite? If the subclass is not used, the parent class is available.

What if the permissions are the same, if the parent class is still decorated with static? A compilation, error.

Immediately followed by another form of writing, the subclass is static decoration, the parent class does not, the DOS result is displayed, is also an error. The next two situations are different, one is "overridden method" and the other is "overridden method".

If both sides are static, there is no problem. DOS display compilation is possible.

Therefore, the following considerations are noted. The second kind of rare, to polymorphic time, to explain to you, why is such a thing. For static methods, we say overwrite, not particularly accurate, because it is no longer dependent on the object, it depends on the class. Which class to use which content.

Preliminary understanding of java--eighth-inheritance-member functions in child-parent classes-Overrides

Related Article

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.