• notice
  • Congratulations on the launch of the Sought Tech site

Interviewer: What is the difference between this and super? Can this be called to the parent class?

This article has included "Java Common Interview Questions": https://gitee.com/mydb/interview

Both this and super are common keywords in Java. Although both can be omitted in many cases, their role in Java is indelible. They are all used to refer to. The reason why each class can call the Object class (the Object class is the parent class of all classes) when it is instantiated is all the "credit" of both.

1.super keyword

super is used to access the properties and methods of the parent class instance.

1.1 super method use

If each instance class does not have a specified construction method shown, it will generate a hidden no-parameter construction method. The same is true for the super() method. 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 of the "every The reason why a class can call the Object class when it is instantiated 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, the corresponding execution code is added to the program.

public class SuperExample {
     // Test Methods
     public static void main(String[] args) {
         Son son = new Son();
     }
}

/**
  * father
  */
class Father {
     public Father() {
         System.out.println("Execute the construction method 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 result is as follows:
image.png
From the above print result, it can be seen that the subclass Son does not display the specified super() method. , Actually called the no-parameter construction method of the parent class, which verifies from the side that if the subclass does not display the specified super() method, then it will also generate a hidden super() method. This point can also be confirmed from the bytecode file generated by this type, as shown in the following figure:
image.png

Super method notes

If the super() method is displayed, then 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:
image.png
As you can see in the above figure, if the super() method is not placed in the first line , Then the compiler will report an error: the super() method must be placed in the first line of the constructor.
Why put the super() method in the first line?
This is because, as long as the super() method is placed in the first line, it can be ensured that the parent class has been initialized first when the subclass is instantiated.

1.2 Use of super attribute

Using super, you can also call the properties of the parent class. For example, the following code can call the age property in the parent class through the subclass Son. The implementation code is as follows:

public class SuperExample {
     // Test Methods
     public static void main(String[] args) {
         Son son = new Son();
     }
}

/**
  * father
  */
class Father {
     // Define an age property
     public int age = 30;

     public Father() {
         super();
         System.out.println("Execute the construction method of the parent class");
     }
}

/**
  * Subclass
  */
class Son extends Father {
     public Son() {
         System.out.println("parent class age:" + super.age);
     }
}

The execution result of the above program is shown in the figure below. The age attribute in the parent class is successfully obtained in the subclass:
image.png

2.this keyword

This is used to access the properties and methods of the instance of this class. It will first find it in this class, and if it cannot be found in this class, it will find it in the parent class.

2.1 Use of this attribute

The most common usage of this is to assign properties of this class, such as a common setter method, as shown in the following code: In the
image.png
above code, this.name represents the name property of the Person class. The this keyword here cannot be omitted . If it is omitted, It is equivalent to assigning a value to the current local variable name, and assigning a value to itself. We can try it, cancel the 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("Lei Ge");
         System.out.println(p.getName());
     }
}

The execution result of the above program is shown in the following figure:
image.png
From the above result, it can be seen that after the this keyword is removed, the assignment fails, and the name attribute in the Person object is null.

2.2 Use of this method

We can use the this() method to call the construction method in this class. The specific implementation code is as follows:

public class ThisExample {
     // Test Methods
     public static void main(String[] args) {
         Son p = new Son("Java");
     }
}

/**
  * father
  */
class Father {
     public Father() {
         System.out.println("Execute the construction method 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 construction method in this class
         this();
         System.out.println("Subclass has parameter construction method, name:" + name);
     }
}

The execution result of the above program is shown in the figure below:
image.png
It can be seen from the above result that the parameterless construction method in this class is successfully called through the this() method.

Note: The usage rules of this() method and super() method are the same. If the call is displayed, it can only be placed in the first line of the method.

2.3 this access to the parent class method

Next, we try to use this to access the parent class method, the specific implementation code is as follows:

public class ThisExample {
     public static void main(String[] args) {
         Son son = new Son();
         son.sm();
     }
}

/**
  * father
  */
class Father {
     public void fm() {
         System.out.println("The fm() method in the parent class is called");
     }
}

/**
  * Subclass
  */
class Son extends Father {
     public void sm() {
         System.out.println("Call the sm() method of the subclass to access the method of the parent class");
         // call the method in the parent class
         this.fm();
     }
}

The execution result of the above program is as follows:
image.png
As can be seen from the above result, the method in the parent class can be accessed by using this, and this will be found in this class first, and if it is not found, it will be found in the parent class.

3. The difference between this and super

1. Different objects refer to

Super refers to the parent class, which is used to access the parent class; and this refers to the current class.

2. The search range is different

super can only find the parent class, and this will first find it in this class, and if it can't find it, it will go to the super class.

3. This type of attribute assignment is different

This can be used to assign values to the instance properties of this class, but 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){....} to lock, but super cannot achieve this function.

Summarize

Both this and super are keywords in Java, and they both refer to them. When displaying them, you need to put them in the first line of the method (otherwise the compiler will report an error). this represents the current object, super is used to refer to the parent object, and they have four differences: refer to the object, search access, attribute assignment of this class, and the use of synchronized are different.

Right or wrong is judged to oneself, to others, to others, gains and losses can be settled in numbers.
Public Account: Analysis of Real Java Interview Questions


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

lipitor 40mg canada & lt;a href="https://lipiws.top/"& gt;buy atorvastatin 20mg pills& lt;/a& gt; buy atorvastatin 80mg for sale

Xfcwvy

2024-03-08

Leave a Reply

+