Detailed explanation of the use of Super keywords in Java programming _java

Source: Internet
Author: User

By defining a method or member with static, it provides some convenience for us to program, and to some extent it is similar to global functions and global variables in C language. However, it is not said that with this convenience, you can use everywhere, if so, you need to seriously consider whether you are using object-oriented thinking programming, whether your own program is object-oriented. OK, now let's discuss the meaning and usage of the two keywords This&super. In Java, this usually refers to the current object, and Super refers to the parent class. When you want to refer to something of the current object, such as a method of the current object, or a member of the current object, you can use this to do this, and of course, another purpose of this is to call another constructor of the current object, which is about to be discussed immediately. If you want to refer to something of the parent class, it is not super because this is so similar to super and has some kind of inherent relationship, so we are here to discuss,
Hope can help you to distinguish and master them two. The most common scenario in the general approach is that a parameter name in your method has the same name as a member of the current object, and you need to explicitly use the This keyword to indicate that you want to use a member in order to avoid confusion.
The use method is "this. Member name", and the one without this is the formal parameter. In addition, you can refer to a method of the current object with "this. Method name", but this is not necessary, and you can access that method directly using the method name.

Using super in Java classes 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, the subclass object will have a parent class object. How do you refer to the parent object inside? Using Super to refer to, this refers to the current object's reference, and super is a reference to the parent object inside the current object.

Super keyword Test

Package cn.galc.test;
  /** * Parent class * @author GaCl */class Fatherclass {public int value;
    public void F () {value=100;
  System.out.println (the Value property of the parent class = +value); /** * Subclass ChildClass inherits from parent Fatherclass * @author GACL * */class ChildClass extends Fatherclass {/** * Subclass except inheriting parent class
   With the Valu attribute, it declares another value property, * that is, the subclass now has two value properties.
  */public int value;
   /** * In the subclass ChildClass rewrite the implementation of the F () method inherited from the parent class, which overrides the method body of the F () method. */public void F () {super.f ();//Use Super as the reference object of the parent class to invoke the F () method inside the parent class object value=200;//This value is the Valu that the subclass defines itself, not the one inherited from the parent class
    A value System.out.println (the Value property of the subclass = +value); System.out.println (value); The value of that value, which is defined by the subclass, is 200/** * Printed is the value of the parent class, because the subclass is overriding the F () method inherited from the parent class. In a word "super.f ();"
     is to have the parent object's Reference object invoke the F () method of the parent object, * that is, the parent object's own call to the F () method to change its Value property, from 0 to 100.
     * So the value printed here is 100.
  * * SYSTEM.OUT.PRINTLN (Super.value); }/** * Test class * @author gacl * * */public class Testinherit {public statIC void Main (string[] args) {childclass cc = new ChildClass ();
  CC.F ();

 }
}

Run Result:

draw a memory analysis diagram to understand the entire 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:

 


Program execution here, first in the stack space will produce a variable cc,cc inside the value of what this is not easy to say, in short, through this value we can find new out of the Chlidclass object. Since the subclass Chlidclass is inherited from the parent class Fatherclass, when we new a subclass object, the subclass object contains a parent class object that has its own property value. The value member variable was not initialized when it was declared in the Fatherclass class, so the system initializes it to 0 by default, and the member variable (declared in the class) is not initialized to it when declared, and the compiler automatically initializes the member variable. But a local variable (declared in the method) must be initialized when it is declared, because the compiler does not automatically initialize the local variable, and any variable must initialize it before it is used.

When a subclass inherits the parent value attribute, it also defines a value attribute separately, so when we new a subclass object, the object will have two value attributes, one is the value inherited from the parent class, and the other is its own value. The member variable value defined in the subclass is not initialized to it when it is declared, so the compiler initializes it to 0 by default. Therefore, after the first sentence is executed, the system memory is laid out as shown in the following illustration:

Next, execute the second sentence:

 
 


When the new object comes out, the object produces a reference to this, which refers to the object itself. If the new object is a subclass object, then there is also a super reference in the subclass object, which points to the parent object within the current object. So the equivalent program has a this,this pointing to the object itself, and a super,super to the parent object inside the current object.
&NBSP
Here calls the overridden F () method, the first sentence in the body of the method: "Super.f ();" is to have the parent object inside the subclass object call its own f () method to change the value of its property. The parent object calls its own f () method by pointing to his reference super, so after this sentence, the value in the parent object becomes 100. Then execute "value=200" where the Vaule is the value declared by the subclass object itself, not the value inherited from the parent class. So after this sentence is executed, the value of the subclass object itself becomes 200. The memory layout is shown in the following illustration: The last three sentences in the body of the

method are commands that print the value value, and the first two are printed with the value of the subclass object itself, so the result is 200, The last word printed is the parent object inside this subclass object own value value, print out the result is 100.
&NBSP
To this end, the entire memory analysis is finished, and the resulting memory displays as shown above.

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.