The difference between this and super in java

this keyword

 

Usage of this:
 In a normal method, this always points to the object that calls the method.
 In the constructor, this always points to the object being initialized.

 

 this() calls the overloaded constructor to avoid the same initialization code. But it can only be used in construction methods, and
Must be in the first sentence of the constructor.
 this cannot be used in static methods.
 this is an "implicit parameter" of a common method, which is passed into the method by the system.
The usage of this is explained in detail
public class TestThis {

int a, b, c;

TestThis() {
    System.out.println("About to initialize a Hello object");
}

TestThis(int a, int b) {
    // TestThis(); // In this way, the constructor cannot be called!
    this(); // Call the parameterless constructor and must be on the first line!
    a = a;// This refers to local variables rather than member variables
    // This distinguishes between member variables and local variables This situation accounts for the majority of this usage!
    this.a = a;
    this.b = b; 
}

TestThis(int a, int b, int c) {
    this(a, b); // Call the constructor with parameters and must be on the first line!
    this.c = c; 
}

   void sing() {

   }

   void eat() {
       this.sing(); // Call sing() in this class;
       System.out.println("Your mother called you home for dinner!");
   }

public static void main(String[ ] args) {
       TestThis hi = new TestThis(2, 3);
       hi.eat();
    }
 }

super keyword

 

1. super "can be regarded as" is a reference to a direct parent object. You can access the party in the parent class that is overridden by the child class through super
Method or attribute.
2. Use super to call ordinary methods. The statement has no location restrictions and can be called freely in subclasses.
3. In a class, if the first line of the constructor does not call super(...) Or this(...); Then Java defaults
super() will be called, which means to call the parameterless constructor of the parent class.
Use of super keyword
public class TestSuper01 { 
    public static void main(String[ ] args) {
        new ChildClass().f();
    }
}

class FatherClass {
    public int value;
    public void f(){
        value = 100;
        System.out.println ("FatherClass.value="+value);
    } 
}

class ChildClass extends FatherClass {
    public int value;
    public int age;
    public void f() {
        super.f(); //Call the normal method of the parent class
        value = 200;
        System.out.println("ChildClass.value="+value);
        System.out.println(value);
        System.out.println(super.value); //Call the member variable of the parent class
}
    public void f2() {
        System.out.println(age); }

}
The execution results are shown in the figure:

The difference between the two

1. Attribute differences:
This accesses the attribute in this class. If this class does not have this attribute, continue to find it from the parent class. super accesses properties in the parent class.
2. Differences in methods:
This accesses the method in this class. If this class does not have this method, continue to find it from the parent class. super accesses methods in the parent class.
3. Structural differences:
This call to this class construction must be placed in the first line of the construction method. When super calls the parent class construction, it must be placed in the first line of the child class construction method.
4. Other differences:
this represents the current object. super cannot represent the current object
A,this. Variables and super variable
this. The variable of the current object called by the variable;
super. Variables directly call variables in the parent class.
B. This and super methods
This (parameter) calls (forwards) the constructor in the current class;
Super (parameter) is used to confirm which constructor in the parent class to use.
 

Note:

1) When initializing a subclass with a parent class, the constructor of the parent class will also be executed, which takes precedence over the constructor of the subclass; Because the first line in the constructor of each subclass has a default implicit statement super();
2) this() and super() can only be written on the first line of the constructor;
3) this() and super() cannot exist in the same constructor. First, both this () and super () must be written on the first line of the constructor; Second, this () statement calls another constructor of the current class, and there must be a constructor of the parent class in this other constructor. Using super () to call the constructor of the parent class again is equivalent to calling the constructor of the parent class twice, and the compiler will not pass;
4) This and super cannot be used for static modified variables, methods and code blocks; Because this and super refer to objects (instances).
 

 

Keywords: Java webview

Added by jibster on Thu, 23 Dec 2021 21:13:09 +0200