This article has included "Java common interview questions": https://gitee.com/mydb/interview
this and super are common keywords in Java. Although they can be omitted in many cases, their role in Java is indelible. They are all used for reference. The reason why each class can call the Object class when instantiating (the Object class is the parent of all classes) is the "credit" of both.
1.super keyword
super is used to access parent class instance properties and methods.
1.1 use of super method
Each instance class generates a hidden parameterless constructor if it does not have the specified constructor displayed. The super() method is similar. If the specified super() method is not displayed, the subclass will generate a hidden super() method to call the parameterless construction method of the parent class. This is what we said at the beginning, "the reason why each class can call the Object class when instantiating is that the default super method works", Next, let's verify this statement through examples.
PS: the so-called "display" refers to the active call in the program, that is, adding the corresponding execution code in the program.
public class SuperExample { // test method public static void main(String[] args) { Son son = new Son(); } } /** * Parent class */ class Father { public Father() { System.out.println("Execute the constructor of the parent class"); } } /** * Subclass */ class Son extends Father { }
In the above code, the subclass Son does not display the specified super() method. We run the above program and the execution results are as follows:
It can be seen from the above print results that the subclass Son called the parameterless construction method of the parent class without displaying the specified super() method. This verifies from the side that if the subclass does not display the specified super() method, it will also generate a hidden super() method. This can also be confirmed from the bytecode file generated by this class, as shown in the following figure:
super method considerations
If the super() method is displayed, the super() method must be placed in the first line of the construction method, otherwise the compiler will report an error, as shown in the following code:
As shown in the figure above, if the super() method is not placed on the first line, the compiler will report an error: it will prompt that the super() method must be placed on the first line of the constructor.
Why put the super() method on the first line?
This is because as long as the super() method is placed in the first line, it can ensure that the parent class has been initialized first when instantiating the child class.
1.2 use of super attribute
super can also call the attribute of the parent class. For example, the following code can call the age attribute in the parent class through the subclass Son. The implementation code is as follows:
public class SuperExample { // test method public static void main(String[] args) { Son son = new Son(); } } /** * Parent class */ class Father { // Define an age attribute public int age = 30; public Father() { super(); System.out.println("Execute the constructor of the parent class"); } } /** * Subclass */ class Son extends Father { public Son() { System.out.println("Parent class age:" + super.age); } }
The execution results of the above programs are shown in the following figure. The age attribute in the parent class is successfully obtained in the subclass:
2.this keyword
This is used to access the instance properties and methods of this class. It will be found in this class first. If it cannot be found in this class, it will be found in the parent class.
2.1 use of this attribute
The most common usage of this is to assign this type of attribute, such as the common setter method, as shown in the following code:
This. In the above code Name represents the name attribute of the Person class. The this keyword here cannot be omitted. If omitted, it is equivalent to assigning name to the current local variable name, and assigning it to yourself. We can try to cancel this keyword. The implementation code is as follows:
class Person { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } public class ThisExample { public static void main(String[] args) { Person p = new Person(); p.setName("Brother Lei"); System.out.println(p.getName()); } }
The execution results of the above procedures are shown in the figure below:
As can be seen from the above results, after removing the this keyword, the assignment fails, and the name attribute in the Person object is null.
2.2 use of this method
We can use this() method to call the constructor in this class. The specific implementation code is as follows:
public class ThisExample { // test method public static void main(String[] args) { Son p = new Son("Java"); } } /** * Parent class */ class Father { public Father() { System.out.println("Execute the constructor of the parent class"); } } /** * Subclass */ class Son extends Father { public Son() { System.out.println("Parameterless construction method in subclass"); } public Son(String name) { // Use this to call the parameterless constructor in this class this(); System.out.println("Subclass parametric construction method,name:" + name); } }
The execution results of the above procedures are shown in the figure below:
From the above results, we can see that the parameterless construction method in this class is successfully called through the this() method.
Note: the use rules of this() method and super() method are the same. If the displayed call can only be placed in the first line of the method.
2.3 this access parent method
Next, we try to use this to access the parent method. The specific implementation code is as follows:
public class ThisExample { public static void main(String[] args) { Son son = new Son(); son.sm(); } } /** * Parent class */ class Father { public void fm() { System.out.println("Called in parent class fm() method"); } } /** * Subclass */ class Son extends Father { public void sm() { System.out.println("Calling a subclass sm() Method to access the parent class method"); // Calling a method in a parent class this.fm(); } }
The results of the above procedures are as follows:
From the above results, you can access the methods in the parent class by using this. This will be found in this class first. If it cannot be found, it will be found in the parent class.
3. Difference between this and super
1. Refer to different objects
super refers to the parent class, which is used to access the parent class; this refers to the current class.
2. Different search ranges
super can only find the parent class, and this will find it from this class first. If it cannot be found, it will find it from the parent class.
3. The assignment of this type of attribute is different
This can be used to assign values to the instance properties of this class, while super cannot achieve this function.
4.this can be used for synchronized
Because this represents the current object, this can be used for synchronized(this) {...} Lock, while super cannot achieve this function.
summary
This and super are keywords in Java. They both serve as references. When they are displayed and used, they need to be placed in the first line of the method (otherwise the compiler will report an error). This represents the current object, and super is used to refer to the parent object. They are different in four aspects: referring to the object, finding and accessing, assigning the property of this class, and using synchronized.
Right and wrong are judged by ourselves, bad reputation is heard by others, and the number of gains and losses is safe.
The official account: Java interview