Java Foundation-super Keywords

Source: Internet
Author: User

First, super keyword

  

use super in the Java class to refer to the composition of the parent class, use this to refer to the current object , and if a class inherits from another class, when we new the instance object of the subclass, there will be a parent class object inside the subclass object. How do you refer to the parent object inside? Using Super to refer to, this refers to a reference to the current object, and super is a reference to the parent object inside the current object.

1.1.super keyword Test
1  Packagecn.galc.test;2 3 /**4 * Parent Class5  * @authorGaCl6  *7  */8 classFatherclass {9      Public intvalue;Ten      Public voidf () { Onevalue=100; ASystem.out.println ("The value of the parent class =" +value); -     } - } the  - /** - * Subclass ChildClass inherit from parent class Fatherclass -  * @authorGaCl +  * -  */ + classChildClassextendsFatherclass { A     /** at * Subclasses, in addition to inheriting the Valu attribute of the parent class, declare a Value property . - * That is, the subclass at this point has two value properties.  -      */ -      Public intvalue; -     /** - * In subclass ChildClass, the implementation of the F () method inherited from the parent class is rewritten, i.e. the method body of the F () method is rewritten.  in      */ -      Public voidf () { to         Super. f ();//use Super as the reference object of the parent class object to invoke the F () method inside the parent class object +value=200;//This value is the Valu defined by the subclass, not the value inherited from the parent class . -System.out.println ("value of child class =" +value); theSystem.out.println (value);//The value of that value, which is a subclass custom, is printed . *         /** $ * The value is printed in the parent class, since the subclass overrides the F () method inherited from the parent class .Panax Notoginseng * The first sentence, "Super.f ();" is to have the reference object of the parent class call the F () method of the parent class object. - * That is equivalent to the parent object itself called the F () method to change its Value property, changed from 0 to 100.  the * So the value printed here is 100.  +          */ ASystem.out.println (Super. value); the     } + } -  $ /** $ * Test Class -  * @authorGaCl -  * the  */ -  Public classTestinherit {Wuyi      Public Static voidMain (string[] args) { theChildClass cc =NewChildClass (); - cc.f (); Wu     } -}

Operation Result:

  

1.2. Draw a memory analysis diagram to understand the whole process of program execution

Analyzing any program starts with the first sentence of the main method, so first analyze the first sentence in the Main method:

Chlidclass cc = new Chlidclass ();

When the program executes here, first in the stack space will produce a variable cc,cc inside the value is what this is not good to say, in a word, through this value we can find the new Chlidclass object. Because subclass Chlidclass is inherited from the parent class Fatherclass, when we new a subclass object, the subclass object contains a parent object, and the parent object has its own property value. This value member variable is declared in the Fatherclass class and does not initialize it, so the system defaults to initialize it to 0, the member variable (declared within the class) can not initialize it when declaring, the compiler will automatically initialize this member variable, But a local variable (declared inside a method) must be initialized at the time of declaration, because the compiler does not automatically initialize the local variable, and any variable must be initialized before it is used.

Subclasses inherit the Value property of the parent class at the same time, they also define a value property individually, so when we new a subclass object, this object will have two value properties, one is inherited from the parent class value, and the other is its own value. The member variable defined in the subclass value is not initialized to it at the time of declaration, so the compiler defaults to initialize it to 0. Therefore, after executing the first sentence, the layout of the system memory is as follows:

 

Next, execute the second sentence:

1 cc.f ();

When the new object comes out, the object produces a reference to this, which points to the object itself. If the new object is a subclass object, then there is a super reference inside the subclass object, which points to the parent object inside the current object. So the equivalent of a program inside a this,this point to the object itself, there is a super,super point to the current object inside the parent object.

Here is called after overriding the F () method, the first sentence of the method body: "Super.f ();" is to let the parent object inside the subclass object call its own f () method to change the value of its Value property, and the parent object calls his own F () method by pointing to his reference super, so after executing this sentence, the value of values inside the parent object becomes 100. Then execute "value=200;" The vaule here is the value that the subclass object declares itself, not the value inherited from the parent class. So after this sentence has been executed, the subclass object itself value is changed to 200. The memory layout at this point is as follows:

The last three sentences in the body of the method are the commands to print the value values, and the first two lines are printed with the value of the subclass object itself, so the printed result is 200, and the last Word prints the parent object's own value of the subclass object, and prints out the result of 100.

In this case, the entire memory analysis is over, and the final memory display results as shown above.

Java Foundation-super Keywords

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.