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 this subclass, This subclass object will have a parent class object inside it. 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.
- Super is a reference to the immediate parent class object . You can access the methods or properties covered by the quilt class in the parent class through Super.
(Note the difference from this:This is a reference to the current object .) See the introduction to this: Object-oriented programming (eight)--this keywords )
- There are two implicit parameters in each common method (This,super)
- Normal method: No order limit, can be called casually. Super.xxx ();
- In the constructor:
- In the constructor of any class, if the first line of the constructor does not show a call to Super (...), then Java will call Super () by default, as the initialization function of the parent class. So, you're super (); It doesn't matter if you add it or not.
Example
1 PackageCom.gta.testoop.inherit;2 3 /**4 * Parent Class5 */6 classFatherclass {7 Public intvalue;8 //Common Methods9 Public voidf () {Tenvalue=100; OneSystem.out.println ("The value of the parent class =" +value); A } - } - the /** - * Subclass ChildClass inherit from parent class Fatherclass - */ - classChildClassextendsFatherclass { + /** - * Subclasses, in addition to inheriting the Value property of the parent class, declare a Value property . + * That is, the subclass at this point has two value properties. A */ at 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. - */ - Public voidf () { - Super. f ();//use Super as the reference object of the parent class object to invoke the F () method inside the parent class object invalue=200;//This value is the value that the subclass defines itself, not the value that inherits from the parent class . -System.out.println ("value of child class =" +value); toSystem.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 . the * 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. $ * So the value printed here is 100. Panax Notoginseng */ -System.out.println (Super. value); the } + } A the /** + * Test Class - */ $ Public classTestinherit { $ - Public Static voidMain (string[] args) { -ChildClass cc =NewChildClass (); the cc.f (); - }Wuyi the}
Execution Result:
The Value property of the parent class is =100 the value of the child class =200200100
View Code
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.
Refer to the Documentation:
Java Basic Learning Summary--super keywords
Object-oriented programming (eight)--this keywords
Object-oriented Programming (iii)--memory analysis during program execution
Object-oriented Programming (10)--Inherited super keyword and memory analysis