A preliminary understanding of java--the eighth chapter-inheritance-the characteristics of member variables in the child parent class

Source: Internet
Author: User

One.

After talking about inheritance, let's take a look at the inheritance of this knowledge point in the code. There is no relationship between classes and classes when writing code before. Now that there is inheritance, there is a parent-child relationship between classes and classes, so let's look at how the program code behaves.

After the child parent class appears, what exactly does it bring to our code?

(from the above, I think of the classification of functions, what are the types?) What is the difference between different categories ? )

First, start with the member variables and introduce the inheritance relationship in the program. As you can see from this example, there is an inheritance relationship, and the same code that follows is omitted (improves reusability).

Because of inheritance, come directly to use.

We discuss the member variables and, of course, focus on specificity. Look at the following, a variable with the same name in the child's parent class, and when we call the variable, we don't know exactly who it's called.

DOS results show two are all 5, here in a sentence, if the son has, do not seek the father. Including what we talked about earlier, we don't find members. This is an analysis from the surface phenomenon of a truth. In terms of memory space, it is their own space has, do not go outside to look for.

Here's a question, what do we do if we want to manipulate the num=4 in the parent class?
Now that the child has inherited the parent class, there is this num=4 in the child. (Do you want to get this effect without changing the name ?) )

There is an idea that the num=5 in the subclass overrides the num=4 in the parent class. Note : in subclasses, int num=5; num=9; Is the overwrite of the same variable. However, NUM and the parent class in the subclass are exactly two variables, all of which have their own owning.

So exactly how to manipulate num in the parent class can use a keyword,

(Super does not represent the parent object)

The similarity is not the same, the usage imagines, the representative is different.

There is a parent class and a subclass, and this is what this class is written on, and if you don't want to represent this class, use Super to represent the parent class. Compile and run, DOS display is not wrong.

Why does the parent class not represent the parent class object, but rather the parent class space? In code, there is only one subclass object, and there is no parent class object. Therefore, it cannot represent the parent class object, only the owning space that the parent class is loaded in. Why can subclasses get content from the parent class? That's because subclasses hold super references.

Before learning the child parent class, get an object to call a method, this method is in the stack, where there is a called this, with this to bind the calling method of the object. And with the son of the parent class, the sub-class with what to tube the parent class called Dad, it is because wrote extends keyword. By the time, no case, again to find in our parent class, how to find this parent class. When we inherit, we have a super,super that points to extends's parent class.

When extends, the subclass holds super, and the super immediately points to the parent. You can access the contents of the parent class with super.

A preliminary understanding of java--the eighth chapter-inheritance-the characteristics of member variables in the child parent class

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.