In-depth understanding of the relationship between child classes and parent classes

Source: Internet
Author: User

 

The second Summary of Java learning! -- In-depth analysis of child classes and parent classes. I hope you can provide more support!

Memory AnalysisCodeCan understand a language in essence!

Child classes inherit from the parent class into two types:

1. Method inheritance:

The parent class directly transfers its method to the subclass. The premise is that the access range of the method modifier of the parent class is the accessible range of the subclass, however, if the child class has already overwritten the method of the parent class, this method cannot be transferred to the Child class.

2. Inheritance of member variables:

The member variable is a bit amazing. It will not be transferred from the parent class to the Child class, but will be kept in the parent class. This will happen, the Child class and the parent class may have two variables with the same name at the same time.

 

The following code illustrates this relatively complex relationship:

Package COM. text; public class test {public static void main (string [] ARGs) {father a = new father (); chilren B = new chilren (); father C = new chilren ();. getage (); system. out. println (. age); B. getage (); system. out. println (B. age); C. getage (); system. out. println (C. age) ;}} class father {int age = 40; Public void getage () {system. out. println (AGE) ;}} class chilren extends father {int age = 18; Public void getage () {system. out. println (AGE) ;}} output 404018181840

The first four results are understandable, but the last two results may ask why the results are different. This code shows the differences between member variable inheritance and method inheritance.

Conclusion can be drawn.:

Father C = new chilren ();

When the above parent class references a subclass object, access variables refer to the reference type, so c. age is a member variable of the parent class, and C. getage () accesses the child chilren method, so the age variable used in this method is the chilren variable.

 In a single sentence, the access variable is declared, and the access method is based on the actual object type (the new type)

 

Next, make some modifications to the code.

 
Public static void main (string [] ARGs) {chilren B = new chilren (); father c = bsystem. out. println (B. age); system. out. println (C. age);} output 1840

Both B and C references point to the same object in the memory, but the printed results are different. This shows that two age values are saved in the memory, one is 18 and the other is 40.

Here we will have some questions about how they are stored in the memory? At this time, we will think of a super keyword, through super. you can access the variables and methods of the parent class. Someone will say: "super. it represents a parent class object, because it points to the parent class. "I thought so before, but after reading some books, I know this is not the case.

In fact, super. is not a "thing", said super. naturally think of this. Some people classify them as similar. In fact, they are very different.

This:Is a real object, representing the current object. You can use return this; to return an object.

Super:It cannot be an object, not a parent class object. Super only modifies the content behind it and tells JVM that this part of content is not the content of the class to which the current object belongs, if return Super is used, JVM is not allowed and is an incorrect syntax.

 
Public static void main (string [] ARGs) {chilren B = new chilren (); father c = bsystem. out. println (B. age); system. out. println (C. age);} output 1840

Return to the code above,This does not mean that there are two objects in the memory, B and C. In fact, there is only one B object in the memory, but C not only has its own instance variables, all instance variables defined by the parent class also exist.

So we can draw a conclusion:When instantiating a subclass, the system allocates memory to all instance variables of the subclass, and allocates memory to the instance variables of its parent class. Duplicate instance variables exist in the parent and child classes in a timely manner, the two will also be allocated with memory. At this time, the subclass only hides the variable of the parent class, but will still allocate memory to it, and then you can use super to access the variable of the parent class.

 

 

 

 

 

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.