“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

1, the introduction of

This and super are the passers to the Object class during class instantiation; This and super are often overlooked in programs because of their implicit use, but understanding their role and usage specifications is definitely necessary. The roles and differences between this and super will be detailed next.

2, the introduction

Let’s start with two examples of error-free code:

package com.liziba.ts; /** * <p> ** @author: / public class Father {private String name; public String getName() { return name; }}Copy the code
package com.liziba.ts; Public class extends Father{public Son(String name) {}}Copy the code

Example of incorrect code for this rule change the Father constructor to an argument constructor:

package com.liziba.ts; /** * <p> ** @author: / public class Father {private String name; public Father(String name) { this.name = name; } public String getName() { return name; }}Copy the code

If the subclass code is not modified, the subclass returns an error:

This is because the Son constructor implicitly calls the super() constructor, which is equivalent to the super() constructor.

But because the parent class does not declare the no-parameter constructor, the no-parameter constructor is overridden by the parameter constructor, and all super() calls cannot reach the parent class. There are two solutions:

1. Declare a no-argument constructor in the parent class

public class Father { public String name; Public Father() {} public Father(String name) {this.name = name; } } public class Son extends Father { public Son(String name) { super(); // can omit}}Copy the code

2. The argument constructor that calls the parent class through super, as shown by subclasses

Public class Son extends Father {public Son(String name) {public class Son extends Father {public class Son(String name) { }}Copy the code

Next, the roles and differences between this and super will be analyzed in detail.

3, this

This is equivalent to the current object instance, or a reference to the current object. This does the following:

  1. Calls methods and properties in the current object
  2. Distinguish object attributes from method parameters
  1. Call constructor (must be on the first line of constructor)

This is equivalent to the current object instance example:

Public class extends Father{private String homework = "Java programming thought "; Public void doSomething() {// this synchronized (this) {}}Copy the code

Examples of calling methods and properties in the current object:

Public class extends Father{private String homework = "Java programming thought "; Public void doSomething() {// this gets the property of the current object. String hn = this.homework; // this calls the current object's method this.doSomething2(); } public void doSomething2() { // toDo } }Copy the code

Example of distinguishing object attributes from method parameters:

Public class extends Father{private String homework = "Java programming thought "; Public Son(String homework) {this.homework = homework; }}Copy the code

Examples of calling other constructors:

Public class extends Father{private String homework = "Java programming thought "; Public Son(String homework) {// Call other constructors, must be on the first line this(homework, "You will all be architects "); } public Son(String homework, String name) { } }Copy the code

4, super

Super can be understood as a reference to an object of the parent (immediate parent, or nearest parent if there are multiple levels of inheritance). Super has the following functions:

  1. Invoke properties and methods that are not private to the parent class
  2. Distinguish between properties and methods of the current class with the same name as the parent class
  1. Call the parent class’s constructor (must be on the first line of the constructor)

Examples of calling parent class properties and methods:

/** * Father */ public class Father {public String name; Public void doSomething3() {// toDo} public void doSomething4() {// toDo} public void doSomething3() {// toDo} public void doSomething4() {// toDo}} public class extends */ Father{public void doSomething() {// Call the non-private method super.dosomething3 (); super.doSomething4(); // Call the parent's non-private property String name = super.name; }}Copy the code

Examples of properties and methods that distinguish the current class from its parent:

/** * Father */ public class Father {public String name; Public void doSomething3() {// toDo} public void doSomething3() {// toDo} public void doSomething4() {// toDo}} public void doSomething3() {// toDo} extends Father { public String name; Public void dosomething() {// super can distinguish a superclass method from the current object's method doSomething3(); doSomething4(); super.doSomething3(); super.doSomething4(); String fatherName = super.name; String sonName = name; } @Override public void doSomething3() { // todo } @Override public void doSomething4() { // todo } }Copy the code

Call the parent class’s constructor (must be on the first line of the constructor)

/** * Father */ public class Father {public String name; public Father(String name) { this.name = name; }} /** * class extends Father {public Son(String name) {super(name); }}Copy the code

5, summary

5.1 Comparative Differences

This basic concept

  • Access the class instance properties and methods

Basic Concept of Super

  • Access parent instance properties and methods

This search scope

  • First look for this class, then look for the parent class if it does not exist

Super search scope

  • Look up the parent class directly

This Other features

  • Used alone to represent the current object

Super Other functions

  • A subclass overrides a method of the parent class to access a method of the same name

5.2 similarities

  • They’re all keywords. They’re all referential
  • Constructors must call other constructors on the first line

5.3 summary figure